Dar*_*noe 59 android android-lifecycle
我需要知道用户何时杀死我的应用程序(强制停止).我一直在阅读android生命周期,它具有onStop()
和onDestroy()
功能,这些与用户在我的应用程序上结束的每个活动相关,但不是在用户强制停止或杀死我的应用程序时.
有没有办法知道用户何时杀死应用程序?
Fai*_*han 120
我找到了一种方法来做到这一点......
像这样做一个服务
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
Run Code Online (Sandbox Code Playgroud)像这样在Manifest.xml中注册此服务
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
Run Code Online (Sandbox Code Playgroud)然后在您的启动活动上启动此服务
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
Run Code Online (Sandbox Code Playgroud)现在每当你从android最近清除你的应用程序然后这个方法 onTaskRemoved()
将执行.
注意:在Android O +中,此解决方案仅在应用程序是前台全职时才有效.在后台使用应用程序超过1分钟后,OnClearFromRecentService(以及所有其他正在运行的服务)将被系统自动强制终止,因此onTaskRemoved()将不会被执行.
Tar*_*nfx 20
没有办法确定进程何时被杀死.从如何检测Android应用程序是否被强制停止或卸载?
当用户或系统强制停止您的应用程序时,整个过程就会被终止.没有回调通知您发生了这种情况.
当用户卸载应用程序时,首先会终止该进程,然后删除您的apk文件和数据目录,以及程序包管理器中的记录,告知其他应用程序您注册了哪些意图过滤器.
创建应用程序类
onCreate()
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
onLowMemory()
This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
onTerminate()
This method is for use in emulated process environments.
Run Code Online (Sandbox Code Playgroud)
即使您被应用程序杀死或强制停止,Android 也会再次启动您的应用程序类
归档时间: |
|
查看次数: |
53187 次 |
最近记录: |