程序锁的核心基本原理

原理图:

程序锁的核心基本原理_第1张图片



基本原理的演示代码:

1、MainActivity

package com.njupt.testlock;

import java.util.List;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		while(true){
			List runningTaskInfos = activityManager.getRunningTasks(1);
			
			RunningTaskInfo info = runningTaskInfos.get(0);
			
			ComponentName topActivity = info.topActivity;
			
			String packageName = topActivity.getPackageName();
			
			System.out.println("packageName: " + packageName);
			
			SystemClock.sleep(1000);	
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



2、AndroidManifest.xml




你可能感兴趣的:(程序锁的核心基本原理)