我们怎么知道Android应用程序已关闭?

我们怎么知道Android应用程序已关闭?,android,android-studio,filepath,Android,Android Studio,Filepath,我正在开发一个Android应用程序,从中下载.mp3文件 服务器。下载完成后,文件将存储在 差异扩展(.srt)。当用户点击播放时 按钮文件的扩展名将更改回.mp3并播放 当用户退出应用程序时,文件将返回到 (.srt)格式 一切正常。但是现在,如果用户不按BackKey关闭应用程序,扩展将是相同的 我的代码是 public void onBackPressed() { new AlertDialog.Builder(this) .setTitl

我正在开发一个Android应用程序,从中下载.mp3文件 服务器。下载完成后,文件将存储在 差异扩展(.srt)。当用户点击播放时 按钮文件的扩展名将更改回.mp3并播放 当用户退出应用程序时,文件将返回到 (.srt)格式

一切正常。但是现在,如果用户不按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活动生命周期进行更多研究:

安卓系统将自动调用某些生命周期方法,例如当用户不按退出按钮就离开应用程序时。您很可能希望在onPause()或onStop()方法中将文件扩展名改回.srt

一旦用户返回应用程序,您可以在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);
 }
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);
 }
然后,您可以通过以下方法从活动的顶部方法开始

protected void onStop(){
super.onStop();
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
或者,您也可以像这样在onStop方法中执行相同的操作

@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);
 }
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()方法。

您可以在方法onStop()上调用函数;或者你可以使用一个服务来执行后台任务,并在你的应用程序进入后台时启动服务。你可以向我建议一个代码。好的,等等,用户没有通过正确的方式退出。你的意思是当应用程序进入后台时?@Android Developer培训生你使用了哪种代码或创建了一个新服务创建了一个新服务。。。并粘贴了上述代码:)实际上,我的建议是使用服务,因为它在后台运行,因为在某个时候,当你在一个活动停止时执行了一个长任务,那么它可能会因为在它前面的另一个活动而停止在中间。所以,当另一个应用程序来到前面创建一个新的服务并粘贴你所给的代码时,我如何继续使用它。但当播放音乐时,如果另一个应用程序出现在前面,它就会停止。如何在后台继续播放音乐?