【问题标题】:Wake up phone when service call Activity and play MediaPlayer, Android服务调用Activity并播放MediaPlayer,Android时唤醒手机
【发布时间】:2023-03-20 13:44:02
【问题描述】:

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

有什么想法吗?

谢谢

【问题讨论】:

    标签: android media-player alarm lockscreen


    【解决方案1】:

    解决了!

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

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

    而且它有效!

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

    【讨论】:

      【解决方案2】:

      在你的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);
      

      【讨论】:

      • 嗯...事实上我试图取下铃声并让振动打开,它现在正在工作......为什么?
      【解决方案3】:

      接受的答案并没有为我解决问题。不过设置延迟确实可以。

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

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多