如何在不打开应用程序的情况下运行警报

时间:2018-06-28 06:33:55

标签: android alarmmanager android-alarms

根据这个问题需要后台服务。

How to keep a service running in background even after user quits the app?

我试图这样做,但是我无法确定应该在服务内致电警报管理器的位置。

MyService.java

这是我尝试的服务等级。

public class MyService extends Service{

    PendingIntent pendingIntent;
    AlarmManager alarmManager;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Query the database and show alarm if it applies

        // I don't want this service to stay in memory, so I stop it
        // immediately after doing what I wanted it to do.
        pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 280192, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 11);
        calendar.set(Calendar.MINUTE, 55);
        calendar.set(Calendar.SECOND,00);

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
//        stopSelf();

        return START_NOT_STICKY;
    }

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

    @Override
    public void onDestroy() {
//        // I want to restart this service again in one minute
//        AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
//        alarm.set(
//                alarm.RTC_WAKEUP,
//                System.currentTimeMillis() + (1000 * 60 * 1),
//                PendingIntent.getService(this, 0, new Intent(this, MyService.class), 0)
//        );
    }
}

即使应用程序未运行,也请指导我在特定时间段提醒用户。 谢谢

2 个答案:

答案 0 :(得分:0)

首先创建如下所示的BroadcastReceiver:

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";

private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit
                                                                // intent
    PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar now = Calendar.getInstance();
    now.add(Calendar.SECOND, 20);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
}

}

现在,我们正在创建第二个广播接收器,以捕获从AlarmManager发出的明确意图。

public class SchedulerEventReceiver extends BroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerEventReceiver.onReceive() called");
    Intent eventService = new Intent(ctx, SchedulerEventService.class);
    ctx.startService(eventService);
}

}

处理服务,例如:

public class SchedulerEventService extends Service {
private static final String APP_TAG = "com.hascode.android.scheduler";

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

@Override
public int onStartCommand(final Intent intent, final int flags,
        final int startId) {
    Log.d(APP_TAG, "event received in service: " + new Date().toString());
    return Service.START_NOT_STICKY;
}

}

这可以帮助您处理定期安排的任务。

答案 1 :(得分:0)

您无需为此用例创建自己的服务,可以为此使用Android框架中的Alarmservice,它可以启动应用程序(即使应用程序未运行)和设置的时间(前提是手机处于开机状态) 。现在要设置警报,可以在任何视图(活动/片段)中进行呼叫。您可以创建一个按钮并设置操作以根据需要设置“警报”。 调用警报时,您将使用待处理的广播接收器执行任何后续操作。要求如下设置“警报”,您可以使用“待播广播”意图来完成任务。并且相信我,即使您的应用程序未运行,它也能正常工作。注意-AlarmBroadCastReceiver应该是清单接收者,即在清单文件中声明。

private void setAlarm(int type) {


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

    // Alarm type
    int alarmType = AlarmManager.RTC;

    Calendar time = Calendar.getInstance();

    time.setTimeInMillis(System.currentTimeMillis());


    switch (type) {

    case 1:
        // Set Alarm for next 20 seconds
        time.add(Calendar.SECOND, 20);
        break;

    case 2:
        // Set Alarm for next 2 min
        time.add(Calendar.MINUTE, 2);
        break;

    case 3:
      // Set Alarm for next 30 mins
        time.add(Calendar.MINUTE, 30);
        break;

    }


    Intent broadcastIntent = new Intent(this, AlarmBroadCastReceiver.class);

    broadcastIntent.putExtras(sourceIntent);

    Random generator = new Random();

    PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(this,
            generator.nextInt(), broadcastIntent,
            PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(alarmType, time.getTimeInMillis(), pendingAlarmIntent);

}