0

我要创建通知并希望在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); 
} 

}它

给出解决方案。 谢谢。

回答

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仅仅是可以在连词与使用的意图的例子

更多参考:http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

希望帮助!

相关问题