我在StackOverflow上找到了答案,但我遇到了一些困难。我有以下代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent,0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i=0; i<packs.size();i++){
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
if((a.flags & ApplicationInfo.FLAG_SYSTEM)==1){
continue;
}
apps.add(p.packageName);
ActivityManager mActivityManager =(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop = ar.topActivity.getClassName();
Context mContext = getApplicationContext();
Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
Intent startHomescreen = new Intent(Intent.ACTION_MAIN);
startHomescreen.addCategory(Intent.CATEGORY_HOME);
startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startHomescreen);
}
我遇到困难的地方是android工作室说apps
中的apps.add(p.packageName);
未定义,activityOnTop
中的activityOnTop = ar.topActivity.getClassName();
也未定义。最后,lockscreen
中的Intent lockIntent = new Intent(mContext, LockScreen.class);
未定义任何人都可以帮助我解决这些问题吗?请记住,这是onCreate
方法的全部内容,我应该将它移到另一个类吗?