Android:是否可以将通知设置为在需要时间通知?即使项目没有运行?

时间:2011-12-23 11:17:00

标签: android notifications android-ndk android-notifications android-notification-bar

在我的应用程序中,我将使用此代码来使用通知:

 private void addDefaultNotification(){
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.icon;
    CharSequence text = "Notification Text";
    CharSequence contentTitle = "Notification Title";
    CharSequence contentText = "Sample notification text.";
    long when = System.currentTimeMillis();

    Intent intent = new Intent(this, NotificationViewer.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new Notification(icon,text,when);

    long[] vibrate = {0,100,200,300};
    notification.vibrate = vibrate;

    notification.ledARGB = Color.RED;
    notification.ledOffMS = 300;
    notification.ledOnMS = 300;

    notification.defaults |= Notification.DEFAULT_LIGHTS;
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

    notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification);
}
是的,我得到了通知。通过调用该函数。但我想要的是,即使应用程序当时没有在设备上运行也应该通知用户,并且应该在所需的时间通知通知。

有可能吗? 如果是,那么请帮助我。 感谢。

编辑:

    public class AlarmNotificationReceiver extends BroadcastReceiver{
    //private Intent intent;
    private NotificationManager notificationManager;
    private Notification notification;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long value1 = intent.getLongExtra("param1", 0);     
        String value2 = intent.getStringExtra("param2");

        addTwoMonthNotification();

    }
}

我已经这样做但不能在该接收器类中创建通知。为什么?我该怎么办?

2 个答案:

答案 0 :(得分:2)

是的,这是可能的。

您需要在AlarmManager中注册您的意图,并使通知接收器类在运行时等待来自AlarmManager的通知。

基本上您需要以下内容:

  1. 通知Intent类(Intent的子类)

    public class MyNotificationIntent extends Intent {
        // basic implementation ...
    }
    
  2. NotificationReceiver类,BroadcastReceiver的子类。如果您收到来自AlarmManger的通知,并且需要运行您的代码来显示通知(您已经拥有这些内容)

    public class AlarmNotificationReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent){
    
            long value1 = intent.getLongExtra("param1", 0);
            String value2 = intent.getStringExtra("param2");
    
            // code to show the notification  
            ... 
            notificationManager.notify(...);
       }
    }
    
  3. 在AlarmManager中注册通知的实用程序功能

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION",
        Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class);
    intent.putExtra("param1", value1);
    intent.putExtra("param2", value2);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeToRun, pendingIntent);
    

答案 1 :(得分:1)

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

AlarmManager类提供对系统警报服务的访问。这些允许您安排应用程序在将来的某个时间运行。当警报响起时,系统会广播已为其注册的Intent,如果目标应用程序尚未运行,则会自动启动它。

你也可以阅读这个:http://android.arnodenhond.com/tutorials/alarm-notification

您应该能够在没有问题的情况下通知您的用户

<强> ADDED

或者你可以这样做:

 new Handler().postDelayed(new Runnable() { public void run() {
           //Shoot your notification here
      }
   }, 1000 * 60 * 5 );

**或**

http://developer.android.com/resources/articles/timed-ui-updates.html

private Runnable mUpdateTimeTask = new Runnable() {
   public void run() {
       final long start = mStartTime;
       long millis = //something;
       int seconds = //something;
       int minutes = //something;
       seconds     =//something;

       mHandler.postAtTime(this,
               start + (((minutes * 60) + seconds + 1) * 1000));
   }
};