2

ActivityManager.RunningTaskInfo谁能解释为什么我在调用这里的代码片段时总是遇到 runtimeException :

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());
4

1 回答 1

2

尝试以下

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" />
于 2014-01-17T18:48:21.660 回答