2017-01-09 90 views
1

這裏是定時器的代碼,一旦它達到0就會播放聲音(定時器工作正常)。問題是聲音仍然存在,即使通過調用MainActivity.javaonPause後繼續播放鬧鐘

我實施onDestroy()SimpleIntentService.java停止聲音,但顯然它從來沒有被稱爲finish()在呼叫Activity。當應用程序暫停時,我該如何讓聲音停止?

這裏是我的MainActivity.java

public class MainActivity extends Activity { 

    private BroadcastReceiver broadcastReceiver; 
    NumberPicker picker; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     picker = (NumberPicker) findViewById(minutePicker); 

     Log.i("TurnToTech", "Project Name - SimpleBackgroundService"); 

     picker.setMinValue(0); 
     picker.setMaxValue(20); 

     broadcastReceiver = new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context arg0, Intent intent) { 
        String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG); 
        Toast.makeText(getApplicationContext(), 
          text, Toast.LENGTH_SHORT).show(); 
      } 
     }; 
    } 

    Intent msgIntent; 

    public void startTimer(View view) { 
     setContentView(R.layout.activity_main); 

     msgIntent = new Intent(this, SimpleIntentService.class); 
     msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, "Alarm: "); 
     msgIntent.putExtra("time", picker.getValue()); 

     startService(msgIntent); 
    } 

    public void onResume() { 
     super.onResume(); 

     IntentFilter filter = new IntentFilter(SimpleIntentService.ACTION_RESP); 
     filter.addCategory(Intent.CATEGORY_DEFAULT); 

     registerReceiver(broadcastReceiver,filter); 
     } 

     public void onPause() { 
      finish(); 
      unregisterReceiver(broadcastReceiver); 
      super.onPause(); 
     } 
} 

而且SimpleIntentService.java

public class SimpleIntentService extends IntentService { 
    public static final String PARAM_IN_MSG = "in_msg"; 
    public static final String PARAM_OUT_MSG = "out_msg"; 
    int time; 

    public static final String ACTION_RESP = "org.turntotech.intent.action.MESSAGE_PROCESSED"; 

    public SimpleIntentService() { 
     super("SimpleIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     System.out.println("SimpleIntentService Called"); 
     String msg = intent.getStringExtra(PARAM_IN_MSG); 
     int time = intent.getIntExtra("time", 0); 

     // Timer implementation 
     if (time == 0){ 
      playSound(); 
     } 

     while(time > 0){ 

      SystemClock.sleep(5000); // 5 seconds 
      time -= 5; 
      String resultTxt = msg + time + " seconds remaining"; 
      Intent broadcastIntent = new Intent(); 

      broadcastIntent.setAction(ACTION_RESP); 
      broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); 
      broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt); 
      broadcastIntent.putExtra("time", time); 

      sendBroadcast(broadcastIntent); 
      if (time == 0) { 
       playSound(); 
      } 
     } 
    } 

    Uri alert; 

    public void playSound(){ 
     alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), alert); 
     r.play(); 
    } 

    public void onDestroy() { 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), alert); 
     r.stop(); 

     super.onDestroy(); 
    } 
} 

回答

1

在你IntentService你沒有真正停止同一報警的onDestroy功能。因爲每次你得到一個新的實例。

所以我想建議保持一個公共的靜態變量Ringtone,以便它可以從任何地方訪問。在MainActivity中聲明它們。

public static Ringtone r; 
public static Uri alert; 

MainActivityonCreate函數初始化它們。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // ... Other statements 

    // Initialize ringtone here 
    initializeRingtone(); 
} 

private void initializeRingtone() { 
    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    r = RingtoneManager.getRingtone(getApplicationContext(), alert); 
} 

現在你MainActivityonPause()功能應該是這樣的

public void onPause() { 
    unregisterReceiver(broadcastReceiver); 
    r.stop(); 
    super.onPause(); 
} 

如果你希望你繼續從後臺應用程序後,然後時間用完播放聲音,你可能會考慮做這樣的事情在你的MainActivity

public void onResume() { 
    super.onResume(); 
    registerReceiver(broadcastReceiver); 
    initializeRingtone(); // Initialize it again. 
} 

而且012 onResume功能在IntentService中的函數可能看起來像這樣。

public void playSound(){ 
    // Initialize the alert and ringtone again. 
    MainActivity.alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    MainActivity.r = RingtoneManager.getRingtone(getApplicationContext(), alert); 

    MainActivity.r.play(); 
} 

public void onDestroy() { 
    MainActivity.r.stop(); 
    super.onDestroy(); 
} 

希望有所幫助!

+0

非常感謝,但不幸的是,當以這種方式實施時,聲音不會開始播放。定時器用完後就死定了,任何想法爲什麼? –

+0

這裏的實際場景是什麼?你在後臺執行了應用程序,計時器停止播放了嗎?然後,你又恢復了應用程序,你期望在定時器耗盡後播放聲音? –

+0

請參閱最新的答案。這可能有幫助。 –