2014-09-10 100 views
0

我有一個應用程序,並希望每天在特定時間發送通知。 這是我的代碼:在android中發送通知

AlarmManager am = (AlarmManager)getBaseContext().getSystemService(alarm); 
    Intent intent = new Intent("WAKE_UP"); 
    PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0); 

time use AlarmManager.set(). 

    am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender); 

    // Prepare intent which is triggered if the 
    // notification is selected 
    Intent intent2 = new Intent(this, NotificationReceiverActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent2, 0); 

    // Build notification 
    // Actions are just fake 
    Notification noti = new Builder(this) 
      .setContentTitle(" boghche " + "shake") 
      .setContentText("Shake kon :/").setSmallIcon(R.drawable.icon) 
      .setContentIntent(pIntent) 
      .build(); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // hide the notification after its selected 
    noti.flags |= Notification.FLAG_AUTO_CANCEL; 

    notificationManager.notify(0, noti); 

但我不能在特定的時間發送。

+0

http://stackoverflow.com/questions/23440251/how-to-repeat-notification-daily-on-specific-time-in-android通過背景/ 23440985#23440985 – 2014-09-10 10:02:22

+0

什麼是API級別? – Skynet 2014-09-10 10:04:20

回答

0

首先設置報警管理如下

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 18); 
calendar.set(Calendar.MINUTE, 30); 
calendar.set(Calendar.SECOND, 0); 
Intent intent1 = new Intent(MainActivity.this, AlaramReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

在此創建廣播接收器級 「AlaramReceiver」 提高 通知時的onReceive

public class AlaramReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(context, EVentsPerform.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
       context).setSmallIcon(R.drawable.applogo) 
       .setContentTitle("Alaram Fired") 
       .setContentText("Events To be PErformed").setSound(alarmSound) 
       .setAutoCancel(true).setWhen(when) 
       .setContentIntent(pendingIntent) 
       .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
     notificationManager.notify(MID, mNotifyBuilder.build()); 
     MID++; 

    } 

} 

在清單中,您可以添加以下權限此

<!-- permission required to use Alarm Manager --> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 

<!-- Register the Alarm Receiver --> 
<receiver android:name="com.example.alarammanagernotifcation.AlaramReceiver"/> 
+0

我應該在哪裏設置鬧鐘管理器? – programmer 2014-09-10 15:40:19

+0

在你的主類 – 2014-09-11 04:05:06

+0

中oncreate函數?????? :O – programmer 2014-09-11 05:10:48