Android:检查应用程序是否在接收器中被杀死(在最近的应用程序中刷过) [英] Android: Check if app is killed (swipe in recent apps) in a receiver
问题描述
正如标题中所述,我想知道BroadcastReceiver是否有可能知道该应用是否被杀死.
as stated in the headline, I want to know if it is possible for the BroadcastReceiver to know if the App is killed.
我试图通过ActivityManager.RunningAppProcessInfo来实现:
I tried to accomplished that with ActivityManager.RunningAppProcessInfo:
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
boolean isInForeground = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
boolean isInBackground = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE;
boolean cached = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED;
boolean perceptile = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
boolean sleeping = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING;
boolean service = myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
但是这些属性都不能告诉我我的应用程序是否仍然有效,它们只能给我提示.
But none of those attribute can tell me if my App is still alive, they can only give me hints.
通常我会使用getRunningTask(我知道它已被弃用):
Normally I would use getRunningTask (I know it is deprecated):
ActivityManager manager = (ActivityManager) mActivity.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(100);
for(int i = 0; i < runningTaskInfo.size(); i++) {
ComponentName componentInfo = runningTaskInfo.get(i).topActivity;
if(componentInfo.getPackageName().equals(packageName)){
return true;
}
}
但是对于此解决方案,我需要一个我没有做却无法创建的活动(也许您有一个主意)?
But for this solution I need a activity which I dont have and I cant create (Perhaps you have an idea)?
所以我的问题是:是否有可能知道您的应用是否被杀死?
So my question: Is it possible to know if your App is killed or not?
谢谢
推荐答案
像这样创建新服务,
public class FinalizingOperationsService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("FOService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("FOService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("FOService", "Service Ends");
// write server updation code here
// after completing code perform stopself() to stop this service;
}
}
并在清单文件中定义此服务,
And define this service in manifest file like this,
<service android:name=".FinalizingOperationsService" android:stopWithTask="false"/>
然后在您的项目中创建一个应用程序类,并在该类中启动服务
And then create an application class in your project and start service in this class
public class MyApplication extends Application
{
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
}
@Override
public void onCreate()
{
super.onCreate();
Intent itent = new Intent(this, FinalizingOperationsService.class);
startService(itent);
}
}
说明:当应用程序被杀死或破坏时,将调用服务的onTaskRemoved()方法,因此您可以在此方法中执行最终操作.希望对您有帮助.
Explanation: onTaskRemoved() method of the service is called when application is killed or destroyed, so in this method you can perform your final operations. Hope it will help you.
这篇关于Android:检查应用程序是否在接收器中被杀死(在最近的应用程序中刷过)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!