2012-03-16 91 views
0

这是我的代码。 离我是不能够通过编程产生报警..在android中生成警报和通知问题

Calendar cal = Calendar.getInstance(); 
    int id = (int) cal.getTimeInMillis(); 
    Intent myIntent = new Intent(this,MyScheduledReceiver.class); 
    myIntent.putExtra("taskTitle", taskTitle.getText().toString()); 
    myIntent.putExtra("taskDetails", taskDetails.getText().toString()); 

    cal.setTimeInMillis(System.currentTimeMillis()); 
    cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute()); 
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id, 
      myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA); 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

我在广播reciever清单文件

<receiver android:name=".MyScheduledReceiver"></receiver> 

的声明。

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
NotificationManager manger = (NotificationManager)  
context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note", 
System.currentTimeMillis()); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 
NOTIFICATION_ID, 
new Intent(context, MyScheduledReceiver.class), 0); 
     Bundle extras=intent.getExtras(); 
     String title=extras.getString("taskTitle"); 
    String note=extras.getString("taskDetails"); 
     notification.setLatestEventInfo(context, note, title, contentIntent); 
     notification.flags = Notification.FLAG_INSISTENT; 
     notification.defaults |= Notification.DEFAULT_SOUND; 
    manger.notify(NOTIFICATION_ID++, notification); 
+0

我有点困惑 - 你提到你无法产生警报,我认为你的Receiever的onReceive(...)永远不会被调用?是这样吗? – Nick 2012-03-16 14:09:11

+0

好的离开它,如果我想生成通知而不是它是正确的方式......这是一种提醒..我想你明白了我的观点。任务任务通知2012年3月17日下午7:00 – 2012-03-16 14:28:45

+0

我发现在你的闹钟代码中没有明显的错误,所以很可能它的其他类似于你设置的闹钟时间不是你所期望的或者你的Receiver没有做的你期望什么等等。为了简单起见,我只是在你的Receiver的onReceive(...)中显示一个toast消息。我也会注释掉你的cal.set(...)调用,并将你的cal.setTimeInMillis改为cal.setTimeInMillis(System.currentTimeMillis()+ 5000);所以当你运行你的应用程序时,如果你没有看到Toast味精,你就知道它没有启动。 – Nick 2012-03-16 14:54:06

回答

0

我有类似的东西,它的工作原理。

首先喜声明挂起:

Intent intent = new Intent(Global.a, EventAlarmReceiver.class); 
    intent.putExtra("title", ""+cd.title); 
    intent.putExtra("desc", ""+cd.description); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent); 

和EventAlarmReceiver i类有:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); 

    String text = String.valueOf(intent1.getCharSequenceExtra("title")); 

    Notification notification = new Notification(R.id.icon, 
      text, System.getTimeInMillis()); 

    notification.vibrate = new long[]{100,250,300,330,390,420,500}; 

    // Hide the notification after its selected  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent intent = new Intent(context, ShowEventActivity.class); 
    intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title"))); 
    intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc"))); 


    String text1 = String.valueOf(intent1.getCharSequenceExtra("desc")); 

    PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    notification.setLatestEventInfo(context, text, text1, activity); 

    notificationManager.notify((int)System.getTimeInMillis(), notification); 

确保在清单声明声明包,其中接收器是:例如,在我的情况EventAlarmReceiver类是包com.app.name.notifications,所以在清单中我有:

<receiver android:name=".notifications.EventAlarmReceiver"></receiver>