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.
Everything works Fine . But now if the user close the application without pressing BackKey the extension will be the same.
my code is
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();
}
if the user not Exiting through the proper way the function inside the exit button not works.
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.