2016-10-26 49 views

回答

1

使用此代碼。

public class CheckRunningApplicationReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context aContext, Intent anIntent) { 
     try {    
      ActivityManager am = (ActivityManager) aContext 
        .getSystemService(Context.ACTIVITY_SERVICE); 
      List<ActivityManager.RunningTaskInfo> alltasks = am 
        .getRunningTasks(1); 
      for (ActivityManager.RunningTaskInfo aTask : alltasks) { 

       if (aTask.topActivity.getClassName().equals("com.android.phone.InCallScreen") 
        || aTask.topActivity.getClassName().equals("com.android.contacts.DialtactsActivity")) 
       { 
        Toast.makeText(aContext, "Phone Call Screen.", Toast.LENGTH_LONG).show(); 
       } 
       String packageName = "com.example.checkcurrentrunningapplication"; 

       if (aTask.topActivity.getClassName().equals(
         packageName + ".Main")) 
       { 
        // When opens this example screen then show a alert message 
        Toast.makeText(aContext, "Check Current Running Application Example Screen.", 
            Toast.LENGTH_LONG).show(); 
       } 
      } 

     } catch (Throwable t) { 
      Log.i("THROW", "Throwable caught: " 
         + t.getMessage(), t); 
     } 
    } 
} 

在清單中添加此:

<receiver android:name = ".CheckRunningApplicationReceiver"/> 
<uses-permission android:name="android.permission.GET_TASKS" /> 
+0

Android Studio中告訴我,android.permission.GET_TASKS和getRunningTasks(1)已被棄用。是否有可能出於安全原因無法訪問當前正在運行的類的名稱?謝謝。 – DaveKiss

+0

http://stackoverflow.com/questions/31156313/activitymanager-getrunningtasks-is-deprecated-android –

相關問題