AlarmManager在关闭的应用程序下不起作用

时间:2018-07-16 09:17:21

标签: java android alarmmanager intentfilter alarm

我正在尝试制作一个警报应用程序,到目前为止,当该应用程序在前台和后台运行时,它仍然可以正常工作。但是,如果我关闭应用程序(在正在运行的应用程序上轻扫),则警报不会响起。

这是我的代码

Manifest.xml

<receiver
    android:name=".AlarmReceiver">
    <intent-filter>
        <action android:name="es.monlau.smartschool.AlarmReceiver"/>
    </intent-filter>
</receiver>

MyActivity.java:

Intent intentAlarm = new Intent(this, AlarmReceiver.class);
intentAlarm.setAction("es.monlau.smartschool.AlarmReceiver");
AlarmManager alarmManager = (AlarmManager)this.getApplicationContext()
    .getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

AlarmReceiver.java:

@Override
public void onReceive(Context context, Intent intent){
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();
    if (!isScreenOn) {
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
        wl.acquire(10000);
        PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");

        wl_cpu.acquire(10000);
    }

    sendNotification(context, title, descr, id);
}
private void sendNotification(Context context,String messageTitle, String messageBody, int id) {
    Intent intent = new Intent(context, Alarma.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, id,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    boolean vibrar = settings.getBoolean("pref_notifyVibrar", true);
    boolean sonido = settings.getBoolean("pref_notifySonido", true);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(context, "Alarma")
                    .setSmallIcon(R.drawable.logo_app_monlau)
                    .setContentTitle(messageTitle)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setFullScreenIntent(pendingIntent,true)
                    .setLights(Color.BLUE,1,1);

    if (sonido){
        Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mRingtone = RingtoneManager.getRingtone(context, alarmUri);
        mRingtone.play();
    }

    if (vibrar){
        long[] pattern = {0, 1000, 0, 1000, 0};
        notificationBuilder.setVibrate(pattern);
    }

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager != null) {
        notificationManager.notify(id, notificationBuilder.build());
    }
}

我知道这是一个非常普遍的问题,我已经查看了所有相关文章。

我使用了服务,由另一个广播接收器以BOOT_COMPLETED动作启动,但一切都停止了。我真的不知道该改变什么。

0 个答案:

没有答案