2

我已经尝试解决这个问题好几个小时了,但我仍然不明白。

我的代码非常基本:

广播接收器

 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 的代码没有运行,但为什么?

4

1 回答 1

4

应该:

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

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

<receiver android:name="your.package.name.TimerNotif"></receiver>
于 2012-07-30T13:51:41.953 回答