2012-07-30 61 views
1

我一直在努力解決這個問題,但我仍然沒有得到它。BroadcastReceiver和AlarmManager不能與

我的代碼是非常基本的:

廣播接收器

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的代碼沒有運行,但爲什麼?

+0

好吧,現在完成了! :) – lost17 2012-07-30 14:00:45

回答

3

應該是:

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

因爲".TimerNotif"是完整的類名稱的快捷方式,如果你的類是根包您的應用程序。否則,它應該是:

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

非常感謝!我不知道......我花了那麼多時間思考我的Java代碼有什麼問題。 – lost17 2012-07-30 14:01:48

相關問題