0

这是我试图调用这两种方法创建的通知我不能创建两个通知,以不同的时间,甚至使用使用广播接收机

shownotificationAtNight(hour,min)主类两大类, shownotificationAtMorning(hour,min)

public class MainActivity extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     shownotificationAtNight(22,21); 
     shownotificationAtMorning(10,22); 

    } 

    private void shownotificationAtNight(int hour,int min) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY,hour); 
     calendar.set(Calendar.MINUTE,min); 

     if(calendar.getTime().compareTo(new Date()) < 0) {calendar.add(Calendar.DATE, 1);} 

     Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 

    private void shownotificationAtMorning(int hour,int min) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY,hour); 
     calendar.set(Calendar.MINUTE,min); 

     if(calendar.getTime().compareTo(new Date()) < 0) {calendar.add(Calendar.DATE, 1);} 

     Intent intent = new Intent(MainActivity.this, MorningAlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 
} 

然后,接收器类是与MID创建不同的通知

public class MorningAlarmReceiver extends BroadcastReceiver 
{ 
     int MID = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE); 
     @Override 
     public void onReceive(Context context, Intent intent) 
      { 
      long when = System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) context 
                 .getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent notificationIntent = new Intent(context, ToTask.class); 
       notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


       NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
       context).setSmallIcon(meg) 
       .setContentTitle("Todays Alarm") 
       .setContentText("Get done and add new tasks").setSound(alarmSound) 
       .setAutoCancel(true).setWhen(when) 
       .setContentIntent(pendingIntent) 
       .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
       notificationManager.notify(MID, mNotifyBuilder.build()); 
       MID++; 
     } 
} 
随机数遵循0

其他等级的接收器

class AlarmReceiver extends BroadcastReceiver { 
    int MID = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE); 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(context, ToTask.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


     NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
       context).setSmallIcon(meg) 
       .setContentTitle("Tommorow Plan") 
       .setContentText("Events To be Performed").setSound(alarmSound) 
       .setAutoCancel(true).setWhen(when) 
       .setContentIntent(pendingIntent) 
       .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
     notificationManager.notify(MID, mNotifyBuilder.build()); 
     MID++; 
    } 
} 

回答

0

您是否相应地编辑了您的Manifest?

<receiver android:name=".MyBroadcastReceiver"></receiver> 
相关问题