2011-08-16 57 views
6

Bildirim için zaman belirlemeyi öğrenmek istiyorum. Her beş dakikada bir, bildirimi ayarlamak istiyorum, bu yüzden bana yardım et.Her beş dakikada bir android bildirimi göster

public class FirstActivity extends Activity 
{ 
    private static final int HELLO_ID = 1; 
    //public static final int FLAG_AUTO_CANCEL = 0;`enter code here` 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.firstactivity); 

     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.icon; 
     CharSequence tickerText = "Hello"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.statusbarnotification); 
     contentView.setImageViewResource(R.id.image, R.drawable.icon); 
     contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view"); 
     notification.contentView = contentView; 
     notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; 
     Intent notificationIntent = new Intent(getApplicationContext(), SecondActivity.class); 
     //PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.contentIntent = contentIntent; 
     mNotificationManager.notify(HELLO_ID, notification); 
    } 

} 

cevap

3

En iyi yol, bildirimi ayarlayan bir hizmet oluşturmak ve ardından AlarmManager'ı kullanarak hizmeti etkinleştirmek olduğunu düşünüyorum. AlarmManager için kod
:

private void startAlarm() { 
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE); 
    long when = System.currentTimeMillis();   // notification time 
    Intent intent = new Intent(this, ReminderService.class); 
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); 
    alarmManager.setRepeating(AlarmManager.RTC, when, (AlarmManager.INTERVAL_FIFTEEN_MINUTES/3), pendingIntent); 
} 

aralık sabit değerleri kullanmak ya da sadece (milisaniye cinsinden) Kendi değerinizi eklemek kontrol etmek.

public class ReminderService extends IntentService { 
    private static final int NOTIF_ID = 1; 

    public ReminderService(){ 
     super("ReminderService"); 
    } 

    @Override 
     protected void onHandleIntent(Intent intent) { 
     NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     long when = System.currentTimeMillis();   // notification time 
     Notification notification = new Notification(R.drawable.icon, "reminder", when); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= notification.FLAG_AUTO_CANCEL; 
     Intent notificationIntent = new Intent(this, YourActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0); 
     notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent); 
     nm.notify(NOTIF_ID, notification); 
    } 

} 
+1

Teşekkür tür ne istediğini, ancak bu konuda size bir sorum var: Burada

Hizmetidir. 'AlarmManager'ı Servis Sınıfı içinde nasıl ayarlayabilirim, böylece veritabanını kontrol ettiğinde ve bildirimi göstermek için yeni şeyler olduğunda? (Eğer kod açıklayabilirseniz daha iyi olurdu, veritabanının koduna gerek yok ... sadece Servis Sınıfı içindeki "if" ifadesiyle birlikte AlarmManager) –

+1

"StartAlarm()" ne zaman çağrıldı? Herhangi bir bildirim almıyorum .. kod g :( – Neo

İlgili konular