创建一个BroadcastReceiver的通知 [英] Create Notification with BroadcastReceiver

查看:123
本文介绍了创建一个BroadcastReceiver的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建与此code一个通知:

I tried to create a Notification with this Code:

private void setNotificationAlarm(Context context) 
{
    Intent intent = new Intent(getApplicationContext() , MyNotification.class);
    PendingIntent pendingIntent  = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);  

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 , pendingIntent);
    Log.d("ME", "Alarm started");
}

public class MyNotification extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("ME", "Notification started");

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

在这里,我Mainfest声明:

And here my Mainfest declaration:

<receiver
    android:name=".MyNotification"
    android:enabled="true"
    android:exported="false" >
</receiver>

我现在的问题是,生成的报警,但没有显示通知。该BroadcastReceiver的是在mainfest文件中声明,也没有编译器或运行时错误。

My problem now is, that the alarm is generated but the Notification isn't displayed. The BroadcastReceiver is declared in the mainfest file and there are no compiler or runtime errors.

我的第二个问题是, setLatestEventInfo 新的通知构造器是pcated德$ P $。我可以用它代替它呢?

My second problem is that setLatestEventInfo and new Notification Contructor are deprecated. What can I use instead of it?

推荐答案

我认为你需要使用

PendingIntent.getBroadcast (Context context, int requestCode, Intent intent, int flags)

而不是的getService

这篇关于创建一个BroadcastReceiver的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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