[android]OnPause 调用后还会继续播放报警

标签: Java Android
发布时间: 2017/3/19 15:50:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里是一个计时器,一旦它达到 0 (定时器工作正常) 播放声音的代码。问题是声音依然存在甚至通过 onPause()MainActivity.java 调用。

我实现了 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 函数。因为每个 time 你得到的一个新实例。

所以我想建议保持公共静态变量 Ringtone ,它可以访问从无处不在。宣布他们在你 MainActivity

public static Ringtone r; 
public static Uri alert;

初始化它们在 onCreate 的功能你 MainActivity

@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);
}

现在 onPause() 的功能你 MainActivity 应该看起来像这样

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

如果你想要播放的声音,恢复后 application 从背景然后计时器耗尽,你可能会考虑做一些像这样在 onResume 函数的你 MainActivity

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

playSound()函数在 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();
}

希望有所帮助 !

官方微信
官方QQ群
31647020