我一直试图解决这个问题几个小时,但我仍然没有得到它。
我的代码很基本:
广播接收器
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);
}
这是Manifest中的应用程序标记
<receiver android:name="TimerNotif"></receiver>
什么都没发生! TimeNotif中的代码没有运行,但为什么?
答案 0 :(得分:3)
应该是:
<receiver android:name=".TimerNotif"></receiver>
因为".TimerNotif"
是完整类名的快捷方式,所以如果您的类位于应用程序的根包中。否则,它应该是:
<receiver android:name="your.package.name.TimerNotif"></receiver>