我们如何知道 Android 应用程序已关闭?
how can we know that an Android Application is closed?
I am developing an Android Application to download an .mp3 file from
server. When downloading is finished the file will be stored in a
DIFFRENT EXTENSION (.srt) . and when the user clicked on the play
button the file's EXTENSION will be changed back into .mp3 and play
And when the user exit's the Application the file will go back to the
(.srt) format.
一切正常。但是现在,如果用户在不按 BackKey 的情况下关闭应用程序,扩展名将是相同的。
我的密码是
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
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);
System.exit(0);
}
}).create().show();
}
如果用户没有通过正确的方式退出,退出按钮内的功能将不起作用。
您需要在此处对 Android activity 生命周期进行更多研究:https://developer.android.com/training/basics/activity-lifecycle/index.html
Android 系统会自动调用某些生命周期方法,例如当用户离开您的应用程序而没有按退出按钮时。您很可能希望在 onPause() 或 onStop() 方法中将文件扩展名改回 .srt。
一旦用户 returns 进入应用程序,您可以在 onRestart()、onStart() 或 onResume() 方法中恢复扩展。
希望对您有所帮助(我在网站上的第一个回答)!
从 Android Studio 文件->新建->服务->服务创建新服务
然后像这样覆盖 onStartCommand 方法
@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);
}
然后你可以通过这个
从你的activity的onstop方法启动它
protected void onStop(){
super.onStop();
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
或者你可以像这样在 onStop 方法中做同样的事情
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);
}
应在应用程序终止时调用 onDestroy() 方法。
I am developing an Android Application to download an .mp3 file from server. When downloading is finished the file will be stored in a DIFFRENT EXTENSION (.srt) . and when the user clicked on the play button the file's EXTENSION will be changed back into .mp3 and play And when the user exit's the Application the file will go back to the (.srt) format.
一切正常。但是现在,如果用户在不按 BackKey 的情况下关闭应用程序,扩展名将是相同的。
我的密码是
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
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);
System.exit(0);
}
}).create().show();
}
如果用户没有通过正确的方式退出,退出按钮内的功能将不起作用。
您需要在此处对 Android activity 生命周期进行更多研究:https://developer.android.com/training/basics/activity-lifecycle/index.html
Android 系统会自动调用某些生命周期方法,例如当用户离开您的应用程序而没有按退出按钮时。您很可能希望在 onPause() 或 onStop() 方法中将文件扩展名改回 .srt。
一旦用户 returns 进入应用程序,您可以在 onRestart()、onStart() 或 onResume() 方法中恢复扩展。
希望对您有所帮助(我在网站上的第一个回答)!
从 Android Studio 文件->新建->服务->服务创建新服务 然后像这样覆盖 onStartCommand 方法
@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);
}
然后你可以通过这个
从你的activity的onstop方法启动它protected void onStop(){
super.onStop();
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
或者你可以像这样在 onStop 方法中做同样的事情
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);
}
应在应用程序终止时调用 onDestroy() 方法。