我已经尝试解决这个问题好几个小时了,但我仍然不明白。
我的代码非常基本:
广播接收器
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);
}
这进入清单中的应用程序标签
<receiver android:name="TimerNotif"></receiver>
什么都没发生!TimeNotif 的代码没有运行,但为什么?