Error: My app stops working when I use getRunningTasks even when @TargetApi is set to 18

Error: My app stops working when I use getRunningTasks even when @TargetApi is set to 18

正如您在下面的代码中看到的那样,我将目标 api 设置为 18,并且我 运行 进程 getRunningTasks,但它给我一个错误并关闭了我的应用程序。

@TargetApi(18)
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);


ActivityManager.RunningTaskInfo ar = tasks.get(0);
String activityOnTop=ar.topActivity.getClassName();
Intent lockIntent = new Intent(this, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(lockIntent);

Logcat:

01-26 16:30:42.154    1217-1217/com.example.epiclapser.noprocrastinate W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-26 16:30:42.234    1217-1217/com.example.epiclapser.noprocrastinate E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.epiclapser.noprocrastinate/com.example.epiclapser.noprocrastinate.MainActivity2}: java.lang.SecurityException: Permission Denial: getTasks() from pid=1217, uid=10046 requires android.permission.GET_TASKS
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access0(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.SecurityException: Permission Denial: getTasks() from pid=1217, uid=10046 requires android.permission.GET_TASKS
        at android.os.Parcel.readException(Parcel.java:1431)
        at android.os.Parcel.readException(Parcel.java:1385)
        at android.app.ActivityManagerProxy.getTasks(ActivityManagerNative.java:2388)
        at android.app.ActivityManager.getRunningTasks(ActivityManager.java:712)
        at android.app.ActivityManager.getRunningTasks(ActivityManager.java:748)
        at com.example.epiclapser.noprocrastinate.MainActivity2.onCreate(MainActivity2.java:50)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)at android.app.ActivityThread.access0(ActivityThread.java:141)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:5103)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:525)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

在 dalvik.system.NativeStart.main(本机方法)

您的 logcat 说您必须使用权限 "android.permission.GET_TASKS"。

将其放入您的 AndroidManifest.xml 标签之前 <application>

<uses-permission android:name="android.permission.GET_TASKS" />