我开发了一个应用程序,即使在按下主页按钮或切换到另一个活动后也可以播放bgm服务。问题是当我通过最近的应用程序删除应用程序时 - 该服务仍在运行。
当我通过最近的应用删除应用时,如何让它停止?有可能吗?
这是我的代码:
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
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();
}
}
答案 0 :(得分:3)
在您的Manifest中添加stopWithTask属性,您可以在其中定义服务:
<service
android:enabled="true"
android:name=".YourService"
android:exported="false"
android:stopWithTask="true" />
如果上述方法有效,则执行反向
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="false" /> //change to false
因此将调用您的服务方法onTaskRemoved。 (请记住,如果将stopWithTask设置为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();
}
}
答案 1 :(得分:0)
您需要覆盖服务类中的Service.onTaskRemoved(Intent rootIntent)
。当从最近的屏幕中删除属于服务应用程序的任务时,将调用此方法。
这样做:
@Override
public void onTaskRemoved(Intent rootIntent){
stopSelf();
}
希望这会有所帮助。