Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 终止应用程序后未触发AlarmManager通知_Android_Service_Alarmmanager_Localnotification - Fatal编程技术网

Android 终止应用程序后未触发AlarmManager通知

Android 终止应用程序后未触发AlarmManager通知,android,service,alarmmanager,localnotification,Android,Service,Alarmmanager,Localnotification,需要在特定日期和时间将通知显示为剩余通知。要设置余数,我使用AlarmManger设置任务,并使用广播接收器显示通知。但在终止应用程序通知后,其余部分不会触发 要设置日期和时间,请使用此代码 Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class); notificationIntent.putExtra(NOTIFICATION,addAppointDataBinding.e

需要在特定日期和时间将通知显示为剩余通知。要设置余数,我使用AlarmManger设置任务,并使用广播接收器显示通知。但在终止应用程序通知后,其余部分不会触发

要设置日期和时间,请使用此代码

 Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class);
        notificationIntent.putExtra(NOTIFICATION,addAppointDataBinding.edTitle.getText().toString());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR,7);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
服务等级

    public class AlarmIntentService extends Service {
    private static final int NOTIFICATION_ID = 3;
    Context context;
    public AlarmIntentService(Context applicationContext) {
        super();
        this.context = applicationContext;
    }

    public AlarmIntentService() {
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);


        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(Constants.NOTIFICATION_ID,intent.getStringExtra(NOTIFICATION), NotificationManager.IMPORTANCE_HIGH);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            managerCompat.createNotificationChannel(channel);
        }
        Intent notifyIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle(NOTIFICATION_TITLE);
        builder.setContentText(intent.getStringExtra(NOTIFICATION));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);

        builder.setSound(alarmSound);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(Constants.NOTIFICATION_ID);
        }
        Notification notificationCompat = builder.build();
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);

        return START_STICKY;
    }



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

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
广播接收机类

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        AlarmWakeLock.acquire(context);

        Intent intent1 = new Intent(context, AlarmIntentService.class);
        intent1.putExtra(NOTIFICATION,intent.getStringExtra(NOTIFICATION));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent1);
        }else {
            context.startService(intent1);
        }

    }
}

我记得在警报和触发时它是如何工作的方面也有类似的问题。请尝试下面的代码,看看是否有结果

// Depending on the version of Android use different function for setting an Alarm.
// setAlarmClock() => Android < Marshmallow
// setExactAndAllowWhileIdle() => Android >= Marshmallow
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            alarmManager.setAlarmClock(
                AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent),
                pendingIntent
            )
        } else {
            alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.RTC_WAKEUP,
                alarmTimeAtUTC,
                pendingIntent
            )
        }
然后,在广播接收器中:

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.action == "AppointmentBroadcastReceiverAction") {
            sendNotification()
        }
    }
}
步骤2:构建最大优先级通知:[Kotlin中的示例代码;适应Java]

private fun sendInternetDaysLeftNotification(context: Context) {
        val channelId: String = context.resources.getString(R.string.internetDaysLeftNotificationsChannelId)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(%YOUR TITLE%)
            .setContentText(%YOUR TEXT%)
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_MAX)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, DEFAULT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }
// Random.nextInt() can be replaced with any Integer.
            notificationManager.notify(Random.nextInt(0, 100), notificationBuilder.build())
        }

我记得在警报和触发时它是如何工作的方面也有类似的问题。请尝试下面的代码,看看是否有结果

// Depending on the version of Android use different function for setting an Alarm.
// setAlarmClock() => Android < Marshmallow
// setExactAndAllowWhileIdle() => Android >= Marshmallow
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            alarmManager.setAlarmClock(
                AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent),
                pendingIntent
            )
        } else {
            alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.RTC_WAKEUP,
                alarmTimeAtUTC,
                pendingIntent
            )
        }
然后,在广播接收器中:

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.action == "AppointmentBroadcastReceiverAction") {
            sendNotification()
        }
    }
}
步骤2:构建最大优先级通知:[Kotlin中的示例代码;适应Java]

private fun sendInternetDaysLeftNotification(context: Context) {
        val channelId: String = context.resources.getString(R.string.internetDaysLeftNotificationsChannelId)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(%YOUR TITLE%)
            .setContentText(%YOUR TEXT%)
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_MAX)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, DEFAULT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }
// Random.nextInt() can be replaced with any Integer.
            notificationManager.notify(Random.nextInt(0, 100), notificationBuilder.build())
        }

谢谢你的快速回复。但在应用程序死机后它也不工作。@priyankaMore您是否将广播接收器添加到AndroidManifest?是的,是这样的@好的。我建议我们循序渐进,直到找到问题为止。首先,让我们看看报警意图服务。就我个人而言,我从未使用过类似的Alarm Manager,因此消除它的最简单方法是暂时避免它,只需在
AddAppointmentAlarmReceiver
onReceive函数中显示通知即可。我会编辑我的帖子,让你明白我的意思。我按照步骤尝试了你的答案。面对同样的问题。感谢您的快速回复。但在应用程序死机后它也不工作。@priyankaMore您是否将广播接收器添加到AndroidManifest?是的,是这样的@好的。我建议我们循序渐进,直到找到问题为止。首先,让我们看看报警意图服务。就我个人而言,我从未使用过类似的Alarm Manager,因此消除它的最简单方法是暂时避免它,只需在
AddAppointmentAlarmReceiver
onReceive函数中显示通知即可。我会编辑我的帖子,让你明白我的意思。我按照步骤尝试了你的答案。面对同样的问题。