为什么我无法在 BroadcastReceiver 的 OnReceiver 中创建通知?

发布于 2024-12-22 15:25:42 字数 1724 浏览 0 评论 0原文

我将创建通知并希望将其显示在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2024-12-29 15:25:42

你见过这个吗:
来自 BroadcastReceiver 的 startActivity()

看起来您没有使用 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/

希望有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文