AlarmManger set重复停止运行

时间:2018-06-09 19:52:15

标签: android android-alarms android-broadcastreceiver

我想每小时发一次通知。

我在AlarmManager服务中使用setRepeating,问题是当我关闭我的应用程序时,管理器没有广播到我的BroadcastReceiver。

我的BroadcastReceiver:

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        Notification notification = new NotificationCompat.Builder(context, "notify_001")
                .setContentTitle("URL Database")
                .setSmallIcon(R.drawable.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setContentText("You didn't use the URLDatabase app for a while.\nYour urls feel lonely.")
                .setContentIntent(pendingIntent).build();

        NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel channel = new NotificationChannel("notify_001",
                    "URLDatabaseNotification",
                    NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }

        manager.notify(0, notification);
        Log.i("URLDatabase", "Received");
    }
}

我的AlarmManager代码(我在我的活动的onCreate中使用它):

calendar = Calendar.getInstance();
intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) this.getSystemService(ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60, pendingIntent);

我是否需要创建可在后台运行的自定义服务?

1 个答案:

答案 0 :(得分:0)

试试这段代码。 每10分钟开火一次。

广播接收器:

public class AlarmReceiver extends BroadcastReceiver {

public static final int REQUESTED_CODE_ALARM = 101;

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

    // interval in minutes
    long fireTime = System.currentTimeMillis() + 10 * 60000;

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent alarmIntent = new Intent(context, AlarmReceiver.class);

    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(
            context, REQUESTED_CODE_ALARM, alertNewsIntent, 0);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireTime, alarmPendingIntent);
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireTime, alarmPendingIntent);
}}

使用此代码启动闹钟(并首次开火):

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
sendBroadcast(alarmIntent);

停止闹铃:

public void stopAlertAlarm(){
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent alarmReceiver = new Intent(this, AlarmReceiver.class);
    PendingIntent alertNewsPendingIntent = PendingIntent.getBroadcast(
            this, AlarmReceiver.REQUESTED_CODE_ALARM, alarmReceiver, 0);

    alarmManager.cancel(alertNewsPendingIntent);
}

重要提示:不要忘记在清单中注册接收方:

<receiver android:name=".services.AlarmReceiver" />