为什么我无法在 BroadcastReceiver 的 OnReceiver 中创建通知?
我将创建通知并希望将其显示在 BroadcastReceiver 的 onReceiver 上。但我做不到。为什么 ? 我的课程的代码是:
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");
Toast.makeText(context, "Hello! How r u ?", Toast.LENGTH_SHORT).show();
addTwoMonthNotification();
}
private void addTwoMonthNotification(){
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence text = "Your amout is due on this date";
CharSequence contentTitle = "Tax Calculator App";
CharSequence contentText = "Your tax amount is due on the "+System.currentTimeMillis()+"";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, NotificationViewer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
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(NotificationConstants.NOTIFICATION_ID, notification);
}
}
给出解决方案。 谢谢。
I am going to create the notification and want to show it on the onReceiver of the BroadcastReceiver. But i am not able to do it. why ?
The code for my class is:
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");
Toast.makeText(context, "Hello! How r u ?", Toast.LENGTH_SHORT).show();
addTwoMonthNotification();
}
private void addTwoMonthNotification(){
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence text = "Your amout is due on this date";
CharSequence contentTitle = "Tax Calculator App";
CharSequence contentText = "Your tax amount is due on the "+System.currentTimeMillis()+"";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, NotificationViewer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
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(NotificationConstants.NOTIFICATION_ID, notification);
}
}
Give Solution for it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你见过这个吗:
来自 BroadcastReceiver 的 startActivity()
看起来您没有使用
Context.registerReceiver()
因此,您必须将接收器静态添加到清单中(如果您还没有这样做):请确保您的清单中包含以下内容
。上面的
PHONE_STATE
只是一个示例可以与BroadcastReceiver
结合使用的意图更多参考:http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
希望有帮助!
Have you seen this:
startActivity() from BroadcastReceiver
It doesn't look like you are using
Context.registerReceiver()
so you would have to statically add your receiver to the manifest, if you haven't already:make sure you have the following in your manifest
Above,
PHONE_STATE
is just an example of an intent that may be used in conjuntion with theBroadcastReceiver
More reference: http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
Hope that helps!