【问题标题】:Android service call OnDestroy immediately when i start the service in sleep mode当我在睡眠模式下启动服务时,Android 服务会立即调用 OnDestroy
【发布时间】:2016-06-13 17:25:54
【问题描述】:

我使用BroadcastReceiverTo:

  • 叫醒电话
  • 在服务中播放声音和振动
  • 显示Activity

当我在Sleep modeSleep mode

中测试手机不在时,一切都运行良好

但是

当手机在在Sleep mode(手机被锁定,屏幕关闭)时,屏幕亮起,Activity显示良好但振动服务在onStartCommand 之后立即转到OnDestroy!

我的广播接收器:

public class TimerTimeoutBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        WakeLockManager.acquireWakeLock(context);

        Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(closeDialogs);

        // Open OffAlarm Activity
        StartAlarmOffActivity(context);

        //StartVibrationService
        StartVibrationService(context);
    }

    private void StartAlarmOffActivity(Context context) {
        Intent alarmeActivity = new Intent(context, AlarmScreenOffActivity.class);
        alarmeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(alarmeActivity);
    }

    private void StartVibrationService(Context context) {
        Intent playAlarm = new Intent(context, AlarmSoundVibrationService.class);
        context.startService(playAlarm);
    }
}

在振动服务中:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null) {
        stopSelf();
        return START_NOT_STICKY;
    }
    play();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    stop();


    WakeLockManager.releaseWakeLock();
}

在 WakeLockManager 中:

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE , "My Tag");

我的问题:

  • 如何防止在睡眠模式下启动服务时立即调用OnDestroy?
  • 我应该使用通知来保持此服务的运行吗?

编辑 1

我尝试通过通知将该服务作为前台服务:

private void showNotification() {

    CharSequence text = getText(R.string.alarm_vibration_service_started);

    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, AlarmScreenOffActivity.class), 0);

    // Set the info for the views that show in the notification panel.
    Notification notification = null;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {

        notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)  // the status icon
                .setTicker(text)  // the status text
                .setWhen(System.currentTimeMillis())  // the time stamp
                .setContentTitle(getText(R.string.alarm_vibration_service_label)+" "+mTimerTitle)  // the label of the entry
                .setContentText(text)  // the contents of the entry
                .setContentIntent(contentIntent)  // The intent to send when the entry is clicked
                .build();
    }

    // Send the notification.
    startForeground(Alarm_Vibration_SERVICE_Id,  notification);
}

现在我在onStartCommand 中打电话给showNotification,但它对我没有帮助!

我成功了我在 onStartCommand 中使用了Thread.Sleep(10000),在Play()之后我的手机振动了 10 秒。

代码:

private void play() {
    stop();
    mVibrator.vibrate(sVibratePattern, 0);

    enableTimeOut();

    mPlaying = true;
}

public void stop() {
    if (mPlaying) {
        mPlaying = false;
        mVibrator.cancel();
    }
    disableTimeOut();
}

private void enableTimeOut() {
    mHandler.sendMessageDelayed(
            mHandler.obtainMessage(TIMEOUT, true),
            1000 * ALARM_TIMEOUT_SECONDS);
}

在某些情况下,我还测试了上述代码。结果(振动 10 秒)是:

  • 失败: 睡眠模式 => 带有倒计时的Activity ----开始----> TimerTimeoutBroadcastReceiver
  • 失败: 睡眠模式 => A Service ----start----> TimerTimeoutBroadcastReceiver
  • 失败: 正常模式 => A Service ----start----> TimerTimeoutBroadcastReceiver
  • 成功: 普通模式 => 带有倒计时的Activity ----开始----> TimerTimeoutBroadcastReceiver

最后我想在我的应用程序中从Service 开始TimerTimeoutBroadcastReceiver,但这个问题让我很困惑!

【问题讨论】:

    标签: android service wakelock sleep-mode


    【解决方案1】:

    问题解决了!真的,我的服务(以及通知)工作正常,但我在Activity 的onPause 中有此代码:

    protected void onPause() {
        Intent playAlarm = new Intent(this, AlarmVibrationService.class);
        stopService(playAlarm);
        super.onPause();
    }
    

    它 (onPause) 发生在我的 Activity 在睡眠模式下打开之后!我现在不知道为什么会发生这种情况,但它会导致服务停止。现在我删除了上面的代码,问题就解决了……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多