服务电话时唤醒电话活动并播放MediaPlayer,Android

时间:2012-10-27 17:09:06

标签: android media-player alarm lockscreen

我有一个在后台运行的服务,有时此服务正在调用一个新的活动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

有什么想法吗?

感谢

3 个答案:

答案 0 :(得分:2)

解决!

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

所以我所做的就是制作一个asynctask,等待屏幕打开。当发生这种情况时,会响起振铃和振动。

它有效!

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

}

答案 1 :(得分: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);

答案 2 :(得分: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();
    }
}
相关问题