2014-09-10 2 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 -through-background/23440985 # 23440985 –

+0

Что такое уровень API? – Skynet

ответ

0

Сначала установите менеджер Alarm, как показано ниже

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); 

Создание Broadcast Receiver класса "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

+0

в вашем основном классе –

+0

в функции oncreate ?????? : O – programmer

Смежные вопросы