Android 实现通知服务的技术

Android 实现通知服务的技术,android,architecture,service,notifications,Android,Architecture,Service,Notifications,我有一个主要活动,用户可以在其中启用/禁用通知、设置通知间隔以及设置通知间隔将使用的基本时间。通知通常会在两个小时内触发。一段时间后,累加器将达到最大值,不再需要通知 实施此类通知计划的标准方式是什么?我尝试在使用posttime的服务中使用处理程序,但似乎有很多情况会导致它永远无法运行。我查看了服务中的计时器,但将手机置于待机状态会停止任何计时器,而且这似乎是个坏主意 我遇到的另一个选项我还没有探索,但它涉及到使用AlarmManager和BroadcastReceiver。我是否应该放弃服务

我有一个主要活动,用户可以在其中启用/禁用通知、设置通知间隔以及设置通知间隔将使用的基本时间。通知通常会在两个小时内触发。一段时间后,累加器将达到最大值,不再需要通知

实施此类通知计划的标准方式是什么?我尝试在使用posttime的服务中使用处理程序,但似乎有很多情况会导致它永远无法运行。我查看了服务中的计时器,但将手机置于待机状态会停止任何计时器,而且这似乎是个坏主意

我遇到的另一个选项我还没有探索,但它涉及到使用AlarmManager和BroadcastReceiver。我是否应该放弃服务,安排一个重复警报?一旦累加器达到最大值,我需要能够禁用所有剩余报警


感谢您的支持。

如果您启动了一项服务,该服务会产生如下线程:

thread t = new thread(new Runnable(){
    public void Run(){
       boolean notified = false;
       while( !notified ){
          if( notify_time - time > 1000 ){
              Thread.sleep(999);
          else if( notify_time - time <= 0 ){
              // START NOTIFICATION ACTIVITY
              notified = true;
          }
       }
    }
}

t.start();
我个人还没有做过类似的事情,所以我不确定什么样的服务可以通知用户或启动一项活动,但它确实有一项活动的全部可用选项,所以是的


哦,但我刚刚想到,由于这里的多线程特性,您需要使用一个处理程序来处理此问题。

因为我总是有有限数量的通知,并且我可以提前计算经过的时间,所以AlarmManager和BroadcastReceiver的组合似乎工作得很好。下面是我如何实现这一点的:

我首先创建了一个广播接收器

然后,我创建了一个类,该类使用AlarmManager创建/取消向BroadcastReceiver发送消息的报警

重要的部分是使用累加器作为请求代码,以便我们可以在以后取消所有警报


最后,我在活动中使用了NotificationSender类,方法是在onCreate中调用refreshAlarms,以及每当用户修改与计划通知相关的首选项时调用refreshAlarms。重新启动手机将清除所有警报,因此必须重新启动应用程序才能开始通知。如果系统碰巧终止了进程,警报仍将在适当的时间触发。

即使我终止了应用程序,它是否仍会运行?
public class NotificationReceiver extends BroadcastReceiver {

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

        //Get handle to system notification manager
        NotificationManager mNM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        //Get message from intent
        Bundle bundle = intent.getExtras();
        CharSequence text = bundle.getString("notification_message");

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.notification_icon, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(context, context.getText(R.string.app_name),text, contentIntent);

        // Set Flags
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification.
        mNM.notify(R.string.notification, notification);

    }

}
public class NotificationSender {

    private AlarmManager mAlarmManager;
    private Context mContext;
    private Intent mIntent;

    public NotificationSender(Context context){

        this.mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.mIntent = new Intent(context, NotificationReceiver.class);
        this.mContext = context;
    }

    public void setAlarm(Long etaMillis, int accumulator){

        //Create intent to send to Receiver
        this.mIntent.putExtra("notification_message","Message");

        //Use accumulator as requestCode so we can cancel later
        PendingIntent sender = PendingIntent.getBroadcast(this.mContext, accumulator, this.mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        //Set Alarm
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, etaMillis, sender);

    }

    public void cancelAlarms(){

        //requestCode (accumulator) will always be a multiple of 10 and less than 100
        for (int x = 10; x <= 100; x += 10){
            PendingIntent operation = PendingIntent.getBroadcast(this.mContext, x, this.mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mAlarmManager.cancel(operation);
        }

    }

    public void createAlarms(PreferenceHelper prefs){

        //Calculate time notifications are due and set an alarm for each one
        //PreferenceHelper is a class to help pull values from shared preferences
        Date currentTime = new Date();

        for (int i = prefs.getNotificationInterval(); i <= 100; i += prefs.getNotificationInterval()) {

            if (i > prefs.getAccumulator()) {

                this.setAlarm(SystemClock.elapsedRealtime() + calculateETA(i, prefs).getTime() - currentTime.getTime(), i);

            }

        }

    }

    public void refreshAlarms(PreferenceHelper prefs){

        this.cancelAlarms();
        if (prefs.isNotificationsEnabled()) this.createAlarms(prefs);

    }

}