通过最新应用删除应用时停止服务 [英] Stop service when removing app through recent apps
问题描述
我已经开发了一个应用,该应用即使在按下主屏幕按钮或切换到其他活动之后仍可以播放bgm服务。问题是当我通过最近的应用程序删除该应用程序时-该服务仍在运行。
I have developed an app that plays bgm service even after hitting the home button or switching to another activity. The problem is when I remove the app through recent apps - the service is still running.
当我通过最近的应用删除该应用时,如何使其停止?
How do I make it stop when I remove the app through recent apps? Is it possible to do that?
这是我的代码:
public class MixPage extends Activity {
private ToggleButton creekToggle;
SharedPreferences creekPref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mix);
creekToggle = (ToggleButton) findViewById(R.id.creek);
creekPref = getSharedPreferences("creekPref", MODE_PRIVATE);
creekToggle.setChecked(creekPref.getBoolean("is creek on",false));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void playCreek(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Intent objCreek = new Intent(this, PlayCreek.class);
startService(objCreek);
SharedPreferences.Editor editorCreek=creekPref.edit();
editorCreek.putBoolean("is creek on", true);
editorCreek.commit();
Toast.makeText(getApplicationContext(), "Creek is Playing", Toast.LENGTH_SHORT).show();
} else {
Intent objCreek = new Intent(this, PlayCreek.class);
stopService(objCreek);
SharedPreferences.Editor editorCreek=rainPref.edit();
editorCreek.putBoolean("is creek on", false);
editorCreek.commit();
Toast.makeText(getApplicationContext(), "Creek is Stopped", Toast.LENGTH_SHORT).show();
}
}
PlayCrickets.java
PlayCrickets.java
public class PlayCrickets extends Service {
private static final String LOGCAT = null;
MediaPlayer objPlayer;
public void onCreate() {
super.onCreate();
Log.d(LOGCAT, "Service Started!");
objPlayer = MediaPlayer.create(this,R.raw.crickets);
}
public int onStartCommand(Intent intent, int flags, int startId) {
objPlayer.setLooping(true);
objPlayer.start();
Log.d(LOGCAT, "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d(LOGCAT, "Problem in Playing Audio");
return 1;
}
public void onStop() {
objPlayer.stop();
objPlayer.release();
}
public void onPause() {
objPlayer.stop();
objPlayer.release();
}
public void onDestroy(){
objPlayer.stop();
objPlayer.release();
}
@Override
public IBinder onBind(Intent objIndent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
objPlayer.stop();
objPlayer.release();
//stop service
this.stopSelf();
}
}
推荐答案
在您的清单添加stopWithTask属性,您已在其中定义了服务:
In your Manifest add stopWithTask attribute where you have define your service:
<service
android:enabled="true"
android:name=".YourService"
android:exported="false"
android:stopWithTask="true" />
如果上述方法不起作用,那么请反向进行操作
If Above approach doest work then do reverse
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="false" /> //change to false
因此将调用您服务的onTaskRemoved方法。 (请记住,如果将stopWithTask设置为true,它将不会被调用。)
So your service's method onTaskRemoved will be called. (Remember, it won't be called if you set stopWithTask to true).
public class YourService extends Service {
@Override
public void onStartService() {
//your code
}
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want
//stop service
this.stopSelf();
}
}
这篇关于通过最新应用删除应用时停止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!