0

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

 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();

    }
}

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

4

2 回答 2

2

是的,有可能。

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

基本上,您将需要以下内容:

  1. Notification 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);
    
于 2011-12-23T11:41:04.913 回答
1

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

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

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

您应该能够在所需的时间毫无问题地通知您的用户

添加

或者你可以这样做:

 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));
   }
};
于 2011-12-23T11:26:30.523 回答