how can we know that an Android Application is closed?

做~自己de王妃 提交于 2019-12-06 11:53:45

Create a new service from Android Studio File->New ->Service ->Service then override the onStartCommand method like this

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
File file  = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp3"); // handler to your ZIP file
File file2 = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.srt");
 // destination dir of your file
 boolean success = file.renameTo(file2);
 MyService.this.stopService(intent);
    return super.onStartCommand(intent, flags, startId);
 }

then you can start it from onstop method of your activity by this

protected void onStop(){
super.onStop();
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}

or you can do the same in onStop method like this

protected void onStop(){
super.onStop();
File file  = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp3"); // handler to your ZIP file
 File file2 = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.srt");
 // destination dir of your file
 boolean success = file.renameTo(file2);
 }

You'll want to do more research on the Android activity lifecycle here: https://developer.android.com/training/basics/activity-lifecycle/index.html

The Android system will automatically call certain lifecycle methods, such as when the user leaves your app without pressing your exit button. You will most likely want to change your file extension back to .srt in the onPause() or onStop() methods.

Once the user returns to the app, you can revert the extension back in the onRestart(), onStart() or onResume() methods.

Hope that helps (my first answer on the site)!

The onDestroy() method should be called when the application is terminated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!