我有一個背景音樂服務,它工作正常,但當我按下HOME按鈕音樂不會停止。幫我。主頁按鈕服務背景音樂
服務:
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.haha);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
這是我的活動,其中有意向:
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent svc=new Intent(this, BackgroundSoundService.class);//This is the intent
startService(svc);
View adventuretime1 = this.findViewById(R.id.button1);
adventuretime1.setOnClickListener(this);
View advanced2 = this.findViewById(R.id.button2);
advanced2.setOnClickListener(this);
}
@Override
public void onBackPressed(){
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent button1 = new Intent(this, AdventureTime.class);
startActivity(button1);
break;
case R.id.button2:
Intent button2 = new Intent(this, AdvancedKids.class);
startActivity(button2);
break;
}
對於聲音停止,當我點擊是到AlertDialog – 2014-12-02 15:05:43
殺/解除綁定或有停止服務。 HOME按鈕與此有什麼關係?該警報正在後退按鈕上顯示。 Back和Home是不同的按鈕,記住Service不會隨着您的活動而消失。 – Nazgul 2014-12-02 15:20:13
我試圖用Intent替換它svc = new Intent(); \t stopService(svc);但它並沒有停止。 – 2014-12-02 15:28:00