2014-09-25 73 views
2

我需要在特定時間每天顯示通知(例如:4.25PM)。我正在使用AlarmManagerNotification。我在應用程序的着陸頁上設置了警報。我也爲它執行了BroadcastReceiver使用AlarmManager在特定時間顯示通知

代碼設置報警:

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 16); 
calendar.set(Calendar.MINUTE, 25); 
calendar.set(Calendar.SECOND, 0); 

Intent notificationmassage = new Intent(getApplicationContext(),Notificationmassage.class); 

//This is alarm manager 
PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
     AlarmManager.INTERVAL_DAY, pi); 

這是廣播接收器

public class Notificationmassage extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent arg1) { 
     showNotification(context); 
    } 

    private void showNotification(Context context) { 
     Log.i("notification", "visible"); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(context, Notificationmassage.class), 0); 

     NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("xyz") 
       .setContentText("It will contain dummy content"); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1, mBuilder.build()); 
    } 
} 

有什麼不對的這種做法?

+1

嗨。什麼是問題...對不起滾動下來。那麼我能看到的唯一可能會導致問題的是如果這個人沒有打開應用程序會發生什麼。 – Pintac 2014-09-25 11:22:21

+0

嘿,我需要在特定的時間發送通知扔android應用程序,我已經完成上面的代碼,但它不工作.. – 2014-09-25 11:24:53

+1

嗨。定義不工作。它是沒有得到廣播或它不顯示通知 – Pintac 2014-09-25 11:26:34

回答

4

如果您以前沒有這樣做,請將接收者添加到Manifest.xml。

<receiver android:name="org.yourapp.Notificationmassage"></receiver> 
在應用部分

<application> 
     .... 

     <receiver android:name="org.yourapp.Notificationmassage"></receiver> 

    </application> 

如果你已經做到這一點,請編輯您的問題,並顯示您的Manifest.xml

+0

Thanks EveryOne ... – 2014-09-25 15:31:45