2016-02-20 144 views
0

我寫了一個後臺服務,註冊一個「屏幕關閉」廣播接收器。我希望我的服務能夠識別上一個活動應用程序(我的意思是其他應用程序)是否是使用相機的應用程序(例如默認相機,照片奇蹟,谷歌相機,線陣相機等?然後,我可以控制我的服務,它應該做的事,從@FlyingPumba代碼如何檢查相機應用程序是否是服務的活動活動?

調試結果

11月2日至20日:21:53.546 10627-10627/I/CameraApp:活躍APP名稱:我的相機應用 02-20 11:21:53.556 10627-10627/I/CameraApp:相機應用:com.sec.android.app.camera 02-20 11:21:53.556 10627-10627/I/CameraApp:相機應用:com。 google.android.GoogleCamera 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.fotoable.fotobeauty 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.msource.beautyplus 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.venticake.retrica 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.joeware.android.gpulumera 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.ywqc .picbeauty 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:vStudio.Android.Camera360 02-20 11:21:53.566 10627-10627/I/CameraApp:相機應用:com.al malence 。晚上

回答

0

你可以嘗試:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE); 
PackageManager pm = this.getPackageManager(); 
// get the info from the currently running task 
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
Log.d("CameraApp", "Foreground Activity Name ::" + taskInfo.get(0).topActivity.getClassName()); 
ComponentName componentInfo = taskInfo.get(0).topActivity; 
if (isCameraApp(componentInfo.getPackageName())) { 
    // do your stuff 
} 

private boolean isCameraApp(CharSequence packageName) { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // get activities that can resolve the image capture intent 
    List<ResolveInfo> imageCaptureActivites = context.getPackageManager().queryIntentActivities(intent, 0); 
    Log.i("CameraApp","Camera app :" + name.toString()); 
    for(ResolveInfo info : imageCaptureActivites) { 
     if (info.activityInfo.packageName.contains(packageName.toString()) { 
      return true; 
     } 
    } 
    return false; 
} 

您需要在Manifed中獲得以下權限:

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

謝謝@FlyingPumba isCameraApp()函數正常。但活動應用程序名稱並不如預期。它總是我的應用程序。讓我看看我的調試結果。 –

相關問題