Android:是否可以将通知设置为在所需时间通知?即使项目没有运行?
在我的应用程序中,我将使用此代码来使用通知:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。
您需要在 AlarmManager 中注册您的意图,并使通知接收器类在运行时等待来自 AlarmManager 的通知。
基本上,您将需要以下内容:
通知意图类(意图的子类)
NotificationReceiver类,BroadcastReceiver的子类。您从 AlarmManger 收到通知并需要运行代码来显示通知(您已经拥有该内容)
在 AlarmManager 中注册通知的实用程序函数
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:
Notification Intent class (subclass of Intent)
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)
Utility function to register your Notification in AlarmManager
http://android-er.blogspot .com/2010/10/simple-example-of-alarm-service-using.html
AlarmManager 类提供对系统报警服务的访问。这些允许您安排应用程序在将来的某个时间运行。当警报响起时,系统会广播为其注册的 Intent,如果目标应用程序尚未运行,则会自动启动目标应用程序。
您还可以阅读以下内容: http://android.arnodenhond.com/tutorials/alarm-notification< /a>
您应该能够在所需的时间毫无问题地通知您的用户
添加
或者您可以执行以下操作:
** 或 **
http://developer.android.com/resources/articles/timed-ui-updates .html
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:
** OR **
http://developer.android.com/resources/articles/timed-ui-updates.html