BroadcastReceiver和AlarmManager无法使用 [英] BroadcastReceiver and AlarmManager not working with
本文介绍了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无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文