2014-12-02 140 views
0

我有一個背景音樂服務,它工作正常,但當我按下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; 

} 

回答

0

那麼它是一個服務和服務都應該有更長的續航時間,你的UI活動或片段。從您的代碼中,我看不到您正在處理應用程序中的HOME按鈕點擊並停止服務的情況或類似情況。你可能應該這樣做。它只是另一個H/W按鈕,它會觸發你可以搭載的事件。

通過在你的Android代碼做什麼方式System.exit(0);

+0

對於聲音停止,當我點擊是到AlertDialog – 2014-12-02 15:05:43

+0

殺/解除綁定或有停止服務。 HOME按鈕與此有什麼關係?該警報正在後退按鈕上顯示。 Back和Home是不同的按鈕,記住Service不會隨着您的活動而消失。 – Nazgul 2014-12-02 15:20:13

+0

我試圖用Intent替換它svc = new Intent(); \t stopService(svc);但它並沒有停止。 – 2014-12-02 15:28:00

0

因爲當你點擊HOME_BUTTON,服務不會呼叫onDestory()

可以爲HOME_BUTTON註冊一個BroadcastReceiver,像這樣:

context.registerReceiver(new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      //destory the service if you want 
     } 
    }, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); 
+0

我會在那裏放置? – 2014-12-02 15:06:35

+0

對不起,我是新手。你能告訴我,我嘗試過服務類,但是我在你發送的第一行代碼中的「上下文」有錯誤。 – 2014-12-02 15:38:26

0

如果你只是想停止服務,那麼你可以調用this方法。

+0

我試過了,但是,每當我去其他活動,它都會停下來。我不斷需要bgmusic。請幫幫我。 – 2014-12-04 11:34:05