2016-02-20 1 views
0

J'ai écrit un service d'arrière-plan qui enregistre un récepteur de diffusion «écran éteint». J'aimerais que mon service puisse identifier si la dernière application active (je veux dire autre app) est une application qui utilise l'appareil photo ou non (caméra par défaut, photo wonder, caméra google, caméra de ligne, etc.)? ensuite, je peux contrôler mon service à ce qu'il doit faireComment vérifier si une application de caméra est l'activité active d'un service?

résultat de débogage à partir du code @FlyingPumba

2-20 novembre. 21: 53,546 10627-10627/I/CameraApp: active App Nom: mon profil App 02-20 11: 21: 53.556 10627-10627/I/CameraApp: App appareil photo: com.sec.android.app.camera 02-20 11: 21: 53.556 10627-10627/I/CameraApp: App appareil photo: com. google.android.GoogleCamera 02-20 11: 21: 53.566 10627-10627/I/CameraApp: App appareil photo: com.fotoable.fotobeauty 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application Appareil photo: com.commsource.beautyplus 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application Appareil photo: com.venticake.retrica 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application Appareil photo: com.joeware.android.gpulumera 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application Appareil photo: com.ywqc .picbeauty 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application de l'appareil photo: vStudio.Android.Camera360 02-20 11: 21: 53.566 10627-10627/I/CameraApp: application de l'appareil photo: com.almalence .night

Répondre

0

Vous pouvez essayer:

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; 
} 

Vous aurez besoin de cette autorisation dans votre Manifed:

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

Merci @FlyingPumba La fonction isCameraApp() est ok. mais le nom de l'application active n'est pas celui attendu. C'est toujours mon application. Voyons voir mon résultat de débogage ci-dessus. –