0

我使用BroadcastReceiver到:

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

当我在手机不在的情况下进行测试时,一切都运行良好Sleep mode

当手机Sleep mode手机被锁定并且屏幕关闭)时,屏幕会打开并且Activity显示良好振动服务会OnDestroy立即进入onStartCommand

我的广播接收器:

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

现在我打电话给我showNotificationonStartCommand但它对我没有帮助!

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

代码:

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----开始---->TimerTimeoutBroadcastReceiver
  • 失败: 正常模式 => A Service----开始---->TimerTimeoutBroadcastReceiver
  • 成功:正常模式 =>Activity倒计时----开始---->TimerTimeoutBroadcastReceiver

最后我想TimerTimeoutBroadcastReceiverService我的APP中开始,但这个问题让我很困惑!

4

1 回答 1

0

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

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

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

于 2016-03-02T09:59:58.223 回答