1

Я новичок в android. Я хочу установить уведомление локально, которое запускает его в определенное время каждый день. Когда я запустить службу ... Извещение выстреливает сразу ...Как установить уведомление локально с помощью службы и Broadcastreceiver?

я хочу знать, как стрелять уведомления в определенное время с из обжиг немедленно

Шаг 1:

public class startservice extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button=(Button) findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(startservice.this,MainActivity.class); 
      startservice.this.startService(intent); 
     } 
    }); 

    } 

    } 

Шаг 2 : Start Service

public class MainActivity extends Service { 

    AlarmManager am; 
    Calendar calendar; 
    int count=0; 


    @Override 
    public void onCreate() { 
     super.onCreate(); 

     am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      calendar=Calendar.getInstance(); 
      setNotification(); 

    } 
    public void setNotification() { 
      Intent intent = new Intent(this, Startnotification.class); 
      calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire 
      calendar.set(Calendar.MINUTE,12); // Particular minute 
      calendar.set(Calendar.SECOND, 0); 
      double x = Math.random(); 
      String value=String.valueOf(x); 
      intent.putExtra("name", value); 
      sendBroadcast(intent); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, count,intent, PendingIntent.FLAG_UPDATE_CURRENT);  
      am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3600000,pendingIntent); 
     count++; 
     } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

Шаг 3: BroadcastReceiver

public class Startnotification extends BroadcastReceiver { 

    NotificationManager nm; 


    @SuppressWarnings("deprecation") 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     nm = (NotificationManager) context 
     .getSystemService(Context.NOTIFICATION_SERVICE); 
     String getvalue= intent.getStringExtra("name"); 
     CharSequence from = getvalue; 
     CharSequence message = "Android calendar..."; 
     PendingIntent contentIntent = PendingIntent.getService(context, 0, new Intent(),0); 

     Notification notif = new Notification(R.drawable.ic_launcher,"Android calendar...", System.currentTimeMillis()); 
     notif.setLatestEventInfo(context, from, message, contentIntent); 
     nm.notify(1, notif); 
     System.out.println("BroadcastReceiver"); 
    } 
    } 

Как добиться его

Спасибо заранее ...

ответ

0

В setNotification() вы звоните

 sendBroadcast(intent); 

Это вызывает ваш Startnotification.onReceive() немедленно.

Кроме того, вы установите ЧАС в экземпляре календаря, как это:

calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire 

Но вы явно не установить AM индикатор/PM для 12-часового времени (ЧАС используется для установки 12-часовой время). Это означает, что индикатор AM/PM будет по-прежнему настроен на то, что было, когда вы звонили Calendar.getInstance(). Если вы запустите этот код, например, в 10:00 и установите ЧАС на 9, будильник погаснет немедленно, потому что время в прошлом.

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