为什么我不能够创造的BroadcastReceiver的OnReceiver通知? [英] Why i am not able to create the Notification in OnReceiver of BroadcastReceiver?

查看:166
本文介绍了为什么我不能够创造的BroadcastReceiver的OnReceiver通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建通知,并要显示它在广播接收器的onReceiver。但我不能够做到这一点。为什么呢?
在code为我的类是:

 公共类AlarmNotificationReceiver扩展广播接收器{
//私人意图意图;
私人NotificationManager notificationManager;
私人的通知的通知;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    // TODO自动生成方法存根    长值1 = intent.getLongExtra(参数1,0);
    字符串值2 = intent.getStringExtra(参数2);
    Toast.makeText(背景下,你好!怎么řú?,Toast.LENGTH_SHORT).show();
    addTwoMonthNotification();}私人无效addTwoMonthNotification(){
    notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    INT图标= R.drawable.icon;
    CharSequence的文字=你的大写金额是由于在此日期;
    CharSequence的contentTitle =税计算器应用程序;
    CharSequence的contentText = + System.currentTimeMillis的()+您的税额的是由于;
    时长= System.currentTimeMillis的();    意向意图=新意图(这一点,NotificationViewer.class);
    的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,意向,0);
    通知=新的通知(图标,文字的时候);    长[] =振​​动{} 0100200300;
    notification.vibrate =震动;    notification.ledARGB = Color.RED;
    notification.ledOffMS = 300;
    notification.ledOnMS = 300;    notification.defaults | = Notification.DEFAULT_LIGHTS;
    //notification.flags | = Notification.FLAG_SHOW_LIGHTS;    notification.setLatestEventInfo(这一点,contentTitle,contentText,contentIntent);
    notificationManager.notify(NotificationConstants.NOTIFICATION_ID,通知);
}

}

给出解决方案了。
谢谢你。


解决方案

你见过这样的:
<一href=\"http://stackoverflow.com/questions/3849868/startactivity-from-broadcastreceiver\">startActivity()从广播接收器

它看起来并不像你正在使用 Context.registerReceiver()所以你有你的接收机静态添加到清单,如果你还没有:

请确保您已在清单以下

 &lt;接收机器人:名字=。AlarmNotificationReceiver&GT;
    &所述;意图滤光器&gt;
            &lt;作用机器人:名字=android.intent.action.PHONE_STATE/&GT;
    &所述; /意图滤光器&gt;
&LT; /接收器&GT;

以上, PHONE_STATE 就是这样可与广播接收器

更多参考:的http://thinkandroid.word$p$pss.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

希望帮助!

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.

解决方案

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

<receiver android:name=".AlarmNotificationReceiver">
    <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

Above, PHONE_STATE is just an example of an intent that may be used in conjuntion with the BroadcastReceiver

More reference: http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

Hope that helps!

这篇关于为什么我不能够创造的BroadcastReceiver的OnReceiver通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆