错误:当我使用 getRunningTasks 时,我的应用停止工作,即使@TargetApi设置为 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);

日志猫:

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.access$600(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.access$600(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)

你的日志猫说你必须使用权限"android.permission.GET_TASKS"。

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

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

相关内容

  • 没有找到相关文章

最新更新