我写了一个后台服务,用于注册"屏幕关闭"广播接收器。我希望我的服务能够识别最后一个活跃的应用程序(我的意思是其他应用程序)是否是使用相机的应用程序(例如默认相机,照片奇迹,谷歌相机,线路相机等?然后我可以控制我的服务应该做什么。
来自@FlyingPumba代码的调试结果
02-20 11:21:53.546 10627-10627 / I / CameraApp:活动应用名称:我的相机应用 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.commsource.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.almalence.night
答案 0 :(得分: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"/>