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

How run the Alarm without open the Application

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

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

我试过这样做,但我无法确定我应该在哪里调用服务内的警报管理器。

MyService.java

这是我试过的服务class。

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

请指导我在特定时间段向用户发出提醒,即使应用程序不是 运行。 谢谢

首先像下面这样创建 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;
}

}

这可以帮助您处理定期计划的任务。

您不需要为此用例创建自己的服务,您可以为此使用 Android 框架中的 Alarmservice,它可以启动应用程序(即使应用程序不是 运行 ) 和时间设置,前提是 phone 处于 ON 状态。现在要设置闹钟,可以在任何视图中调用 (Activity/Fragment) 。您可以创建一个按钮并设置操作以根据需要设置警报。 在调用警报时,您可以使用挂起的广播接收器进行任何后续操作。调用如下设置警报,你可以使用 Pending Broadcast 意图来做你的事情。相信我,即使您的应用程序不是 运行,它也能正常工作。注意 - 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);

}