繁体   English   中英

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

[英]Android daily notification using alarm manager and service

美好的一天,我想在〜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. 创建警报

    在您的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()); } } 

一些重要的注意事项:上面,我们“ setContentIntent()”,它告诉Android O / S用户单击后要加载的内容。 待处理的Intent必须具有一个新的Intent(),这将启动正确的应用程序。 AutoCancel与正确运行之间存在冲突,所以这就是为什么我们要全局清除MainActivity的所有onStart()通知的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM