2

我有一个在后台运行的服务,在某些时候这个服务正在调用一个新的活动 RingAlarm。

顾名思义,这个活动是一个警报播放器。所以它会播放声音,直到用户按下按钮。

一切都很好,除了我在屏幕锁定时尝试它。然后我发现声音没有播放。

要获取屏幕并解锁它,我正在使用这个:

    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
    wakeLock.acquire();
    KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
    KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();

为了播放铃声,我使用这个:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
     if(alert == null){
         // alert is null, using backup
         alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
             // alert backup is null, using 2nd backup
             alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
         }
     }
     mPlayer = new MediaPlayer();
     try {
         mPlayer.setDataSource(this, alert);
         mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
         mPlayer.setLooping(true);
         mPlayer.prepare();
         mPlayer.start();
     }
     catch(Exception e) {
     //TODO : Implement Error Checking
         e.printStackTrace();
         Log.e("MediaPlayer", "Error while playing!");
     }

也许我做错了什么......知道吗?

更新(错误)

10-28 17:49:59.562: I/MediaPlayer(3193): it is a Ringtone type is : 4
10-28 17:49:59.820: E/RingtoneManager(3193): getActualDefaultRingtoneUri : content://media/internal/audio/media/60
10-28 17:49:59.820: E/RingtoneManager(3193): Uri.parse(uriString) : content://media/internal/audio/media/60
10-28 17:49:59.828: I/MediaPlayer(3193): It is a Not a DRM RingTone: return NULl
10-28 17:49:59.828: I/MediaPlayer(3193): path is null

我仍然有问题。我真的坚持这一点。我发现问题不是媒体播放器,问题是当设备处于锁定模式时,当应用程序再次启动时它没有正确执行,导致振动也不起作用。

我在 logcat 中收到此警告:

10-29 18:10:07.851: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.851: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): beginBatchEdit on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): endBatchEdit on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): beginBatchEdit on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): endBatchEdit on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection

任何的想法?

谢谢

4

3 回答 3

2

解决了!

如果调用媒体播放器或振动时屏幕未打开,则接缝会发生一些错误,并且不会发出声音或振动。

所以我所做的是制作一个异步任务,等待屏幕打开。当这种情况发生时,就会调用振铃和振动。

它有效!

private class AlarmTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        while (!window.isActive()) {}
        if (Preference.readBoolean(getApplicationContext(), Preference.VIBRATION, true))
            vibrate();
        if (Preference.readBoolean(getApplicationContext(), Preference.SOUND, true))
            ring();

        return null;
    }

}
于 2012-10-29T17:50:29.407 回答
1

在你的Activity

试试这个

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
于 2012-10-27T17:14:57.623 回答
0

接受的答案并没有为我解决。不过,设置延迟确实如此。

public static void setRinging(final Ringtone ringtone, final Vibrator vibrator, boolean toRinging) {
    if (toRinging) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ringtone.play();
                vibrator.vibrate(new long[]{1000, 1000}, 0);
            }
        }, 1000);
    } else {
        ringtone.stop();
        vibrator.cancel();
    }
}
于 2018-07-02T16:37:25.757 回答