使用警报管理器和服务的Android每日通知

时间:2015-12-16 08:51:10

标签: android notifications

美好的一天,我想在~10:00每天发出通知。

来自主要活动onCreate()的代码片段:

  Intent mIntent = new Intent(this, NotifyService.class);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, mIntent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 10);


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

这是NotifyService.class:

public class NotifyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this,HomeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
        Notification notification = new Notification.Builder(this)
                .setContentTitle("Horoscope reminder")
                .setContentText("Check your daily horoscope")
                .setSmallIcon(R.drawable.notification_icon)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(9999, notification);
        return super.onStartCommand(intent, flags, startId);
    }

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

在应用程序标记之后从AndroidManifest.xml排成行:<service android:name=".NotifyService"/>

当应用程序运行时,我遇到了这个问题随机推送通知,但是当应用程序关闭时没有任何反应。

无论如何,谢谢!

1 个答案:

答案 0 :(得分:0)

  1. 创建警报

    在您的MainActivity.java中:

     public void setAlarm(){
            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(  MainActivity.this, 0, alarmIntent, 0);
    
            alarmStartTime.set(Calendar.HOUR_OF_DAY, 10);
            alarmStartTime.set(Calendar.MINUTE, 00);
            alarmStartTime.set(Calendar.SECOND, 0);
            alarmManager.setRepeating(AlarmManager.RTC, alarmStartTime.getTimeInMillis(), getInterval(), pendingIntent);
        }
        private int getInterval(){
             int days = 1;
             int hours = 24;
             int minutes = 60;
             int seconds = 60;
             int milliseconds = 1000;
             int repeatMS = days * hours * minutes * seconds * milliseconds;
             return repeatMS;
        }
    
    1. 你会注意到我们有一个Receiver类。这是为了处理警报的广播。创建一个新类AlarmReceiver.java:

      public class AlarmReceiver extends BroadcastReceiver {
      
      NotificationManager notificationManager;
      
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent service1 = new Intent(context, AlarmService.class);
              context.startService(service1);
        }
      

      }

    2. 这只是将意图传递给我们将要创建的另一个类,即服务类。创建服务类(而不是仅仅在onReceive()上执行)使它能够在后台运行。

      public class AlarmService extends Service { 
      private static final int NOTIFICATION_ID = 1;
      private NotificationManager notificationManager;
      private PendingIntent pendingIntent;
      
      @Override
      public IBinder onBind(Intent arg0)
      {
          return null;
      }
      
      @SuppressWarnings("static-access")
      @Override
      public void onStart(Intent intent, int startId)
        {
         super.onStart(intent, startId);
         Context context = this.getApplicationContext();
         notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
         Intent mIntent = new Intent(this, MainActivity.class);
         pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);     
         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
         builder.setContentTitle("Bananas");
         builder.setContentText("get your bananas");
         builder.setSmallIcon(R.drawable.ic_launcher);
         builder.setContentIntent(pendingIntent);
      
         notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
      }
      
  2. 一些重要说明: 上面,我们“setContentIntent()”并告诉Android O / S用户点击后要加载的内容。 Pending Intent必须有一个新的Intent(),这将启动正确的应用程序。 AutoCancel与工作权之间存在冲突,因此我们在全局范围内清除MainActivity的onStart()所有通知。