无法使AlarmManager和多个通知工作

好的..我想弄清楚三个问题。

  1. 我无法让AlarmManager通知我
  2. 我想为每个通知程序显示不同的消息

如果有人能帮助我,我会很高兴,我不会在这里发布这个,所以我可以得到代码,我已经在这里工作了2天所以我想我会问专家。

如果我能找到一个会检查以下代码的人,请表明我的错误并指出我的方向来纠正这些错误。谢谢你。

这是我的通知程序类,我设置了我希望alarmManager发送警报的日期和时间。

public class Notifier extends Activity { private PendingIntent pendingIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the_beginning); Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); Calendar calendar3 = Calendar.getInstance(); Calendar calendar4 = Calendar.getInstance(); calendar1.set(Calendar.DAY_OF_WEEK, 1); // Sunday calendar2.set(Calendar.DAY_OF_WEEK, 4); // Wednesday calendar3.set(Calendar.DAY_OF_WEEK, 6); // Friday calendar4.set(Calendar.DAY_OF_WEEK, 5); // Thursday calendar4.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of Each Month // Sunday calendar1.set(Calendar.HOUR_OF_DAY, 5); calendar1.set(Calendar.MINUTE, 0); calendar1.set(Calendar.SECOND, 0); calendar1.set(Calendar.AM_PM, Calendar.PM); // Wednesday calendar2.set(Calendar.HOUR_OF_DAY, 17); calendar2.set(Calendar.MINUTE, 30); calendar2.set(Calendar.SECOND, 0); calendar2.set(Calendar.AM_PM, Calendar.PM); // Friday calendar3.set(Calendar.HOUR_OF_DAY, 17); calendar3.set(Calendar.MINUTE, 30); calendar3.set(Calendar.SECOND, 0); calendar3.set(Calendar.AM_PM, Calendar.PM); // Thursday calendar4.set(Calendar.HOUR_OF_DAY, 9); calendar4.set(Calendar.MINUTE, 0); calendar4.set(Calendar.SECOND, 0); calendar4.set(Calendar.AM_PM, Calendar.AM); Intent myIntent = new Intent(Notifier.this, MyBReceiver.class); pendingIntent = PendingIntent.getBroadcast(Notifier.this, 0, myIntent, 0); AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE); alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); // every Sunday alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); // every Wednesday alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); // every Friday alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28, pendingIntent); // every first Thursday alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 30, pendingIntent); alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31, pendingIntent); } // end onCreate } 

这是My Receiver类

 public class MyBReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent myservice = new Intent(context, MyAlarmService.class); context.startService(myservice); } } 

这里是我设置通知程序的警报服务(这里只有一个通知程序)我不知道如何为接下来的三个警报设置另一个通知程序。

 public class MyAlarmService extends Service { int currentapiVersion = android.os.Build.VERSION.SDK_INT; private NotificationManager mManager; private Context context; // Notification notification; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public void onStart(Intent intent, int startId) { onStartCommand(intent, 0, startId); } @SuppressWarnings({ "static-access", "deprecation" }) @Override public int onStartCommand(Intent intent, int flags, int startId) { mManager = (NotificationManager) context .getSystemService(context.NOTIFICATION_SERVICE); Intent intent1 = new Intent(this.getApplicationContext(), Notifier.class); if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { Notification notification = new Notification( R.drawable.ic_launcher, "This is a test message!", System.currentTimeMillis()); intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingNotificationIntent = PendingIntent .getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); mManager.notify(0, notification); } else { NotificationCompat.Builder builder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("AlarmManagerDemo1") .setContentText("This is a test message"); Intent notificationIntent = new Intent(this, Notifier.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); } return START_NOT_STICKY; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } } 

谢谢。

为每个警报使用不同的myIntent。 另外,为了避免android优化它,为意图添加不同的数据

 myIntent = new Intent(Notifier.this, MyBReceiver.class); data = Uri.withAppendedPath(Uri.parse("anything" + "://something/different"); myIntent.setData(data);