BroadcastReceiver和AlarmManager无法使用

编程入门 行业动态 更新时间:2024-10-24 02:23:16
本文介绍了BroadcastReceiver和AlarmManager无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经尝试修复了几个小时,但仍然无法解决问题。

I've been trying to fix this for hours but I still don't get it.

我的代码非常基本:

BroadcastReceiver

BroadcastReceiver

public class TimerNotif extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { notificationStatus(context); } private void notificationStatus(Context context) { final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); final int icon = R.drawable.ic_launcher; final Notification notification = new Notification(icon, "test", System.currentTimeMillis()); final Intent notificationIntent = new Intent(context.getApplicationContext(), Main.class); final PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0); notification.setLatestEventInfo(context, "ticker", "title", pIntent); mNotificationManager.notify(1, notification); } }

主要活动

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Calendar cal = Calendar.getInstance(); Intent intent = new Intent(Main.this, TimerNotif.class); PendingIntent pIntent = PendingIntent.getBroadcast(Main.this, 0, intent, 0); AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent); }

然后将其插入到清单中的应用程序标签中

And this into application tag in Manifest

<receiver android:name="TimerNotif"></receiver>

什么都没发生!进入TimeNotif的代码未运行,但是为什么呢?

Nothing happens! The code into TimeNotif is not running, but why?

推荐答案

应该是:

<receiver android:name=".TimerNotif"></receiver>

因为。TimerNotif 是快捷方式完整的类名,如果您的类在应用程序的根包中。否则,应为:

Because ".TimerNotif" is a shortcut for full class name, if your class is in the root package of your application. Otherwise, it should be:

<receiver android:name="your.package.name.TimerNotif"></receiver>

更多推荐

BroadcastReceiver和AlarmManager无法使用

本文发布于:2023-11-27 04:30:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BroadcastReceiver   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!