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

发布于 2024-12-22 21:07:00 字数 1804 浏览 4 评论 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();

    }
}

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

In My application i am going to use this code to use the Notification:

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

rightnow i am getting the notofication. By calling that function. But i want is that it should be notify the user even if the application is not runing on at that time on device and notification should be notify on the desired time.

Is it possible ?
If yes then please help me for that.
Thanks.

Edited:

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

    }
}

I have done like that but not able to create notification in that receiver class. Why ? and what should i have to do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-12-29 21:07:01

是的,这是可能的。

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

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

  1. 通知意图类(意图的子类)

    public class MyNotificationIntent 扩展 Intent {
        // 基本实现...
    }
    
  2. NotificationReceiver类,BroadcastReceiver的子类。您从 AlarmManger 收到通知并需要运行代码来显示通知(您已经拥有该内容)

    公共类 AlarmNotificationReceiver 扩展了 BroadcastReceiver{
    
        @覆盖
        公共无效onReceive(上下文上下文,意图意图){
    
            长值1 = Intent.getLongExtra("param1", 0);
            String value2 = Intent.getStringExtra("param2");
    
            // 显示通知的代码  
            ... 
            notificationManager.notify(...);
       }
    }
    
  3. 在 AlarmManager 中注册通知的实用程序函数

    AlarmManager AlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent意图 = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION",
        Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class);
    Intent.putExtra("参数1", 值1);
    Intent.putExtra("param2", value2);
    
    PendingIntentendingIntent = PendingIntent.getBroadcast(上下文,0,意图,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager.set(AlarmManager.RTC_WAKEUP,timeToRun,pendingIntent);
    

Yes it is possible.

You need to register your intent in AlarmManager and make the notification receiver class to wait for notification from AlarmManager when it's time to run.

Basically you will need the following:

  1. Notification Intent class (subclass of Intent)

    public class MyNotificationIntent extends Intent {
        // basic implementation ...
    }
    
  2. NotificationReceiver class, subclass of BroadcastReceiver. Where you receive notification from AlarmManger and need to run your code to show notification (you already have that stuff)

    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. Utility function to register your Notification in 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);
    
小…红帽 2024-12-29 21:07:01

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

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

您还可以阅读以下内容: http://android.arnodenhond.com/tutorials/alarm-notification< /a>

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

添加

或者您可以执行以下操作:

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

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

AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.

you can also read this one: http://android.arnodenhond.com/tutorials/alarm-notification

You should be able to notify your user at a desired time without problem

ADDED

Or you can do:

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

** OR **

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));
   }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文