谁能解释为什么我在调用ActivityManager.RunningTaskInfo时总是收到运行时异常,这是代码段:

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> listInfo = am.getRunningTasks(2);
    ActivityManager.RunningTaskInfo taskInfo = listInfo.get(0);//it throws exception in this line

    TextView title = (TextView) findViewById(R.id.textView_Settings_Title);
    title.setText(taskInfo.topActivity.getClassName().toString());

最佳答案

尝试以下

final ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningTaskInfo> listInfo = am.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < listInfo.size(); i++)
{
    if (listInfo.get(i).baseActivity.toShortString().indexOf("<yourproject>") > -1) {
        ActivityManager.RunningTaskInfo taskInfo = listInfo.get(i);
    }
}


还可以尝试以下用户权限:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.MANAGE_APP_TOKENS" />
<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

关于android - Android RunningTaskInfo,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19932442/

10-12 05:06