我将创建通知,并希望在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);
}
}
为它提供解决方案。 感谢。
答案 0 :(得分:1)
你见过这个: startActivity() from BroadcastReceiver
看起来您使用的是Context.registerReceiver()
,因此如果您还没有将接收器静态添加到清单中,则必须:
确保您的清单中包含以下内容
<receiver android:name=".AlarmNotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
上面,PHONE_STATE
只是一个可以与BroadcastReceiver
更多参考:http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
希望有所帮助!