2011-12-28 49 views
14

Tôi gặp sự cố trong thông báo thanh trạng thái ở khoảng thời gian 10 giây. Tôi đã thực hiện với mã để hiển thị nó một lần bằng cách tạo plugin.But tôi muốn hiển thị nó sau mỗi 10 phút. sử dụng AlarmManager để tạo thông báo sau mỗi 10 phút. Tuy nhiên, nó không gọi phương thức onReceive(Context ctx, Intent intent) của lớp FirstQuoteAlarm. Tôi có mã sau để hiển thị thông báo và AlarmManager.thông báo trên thanh trạng thái trong điện thoại android

public void showNotification(CharSequence contentTitle, CharSequence contentText) { 
    int icon = R.drawable.nofication; 
    long when = System.currentTimeMillis(); 

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

    Intent notificationIntent = new Intent(ctx, ctx.getClass()); 
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    mNotificationManager.notify(1, notification); 

     Date dt = new Date(); 
     Date newdate = new Date(dt.getYear(), dt.getMonth(), dt.getDate(),10,14,dt.getSeconds()); 
     long triggerAtTime = newdate.getTime(); 
     long repeat_alarm_every = 1000; 
     QuotesSetting.ON = 1; 

     AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); 
     //Intent intent = new Intent("REFRESH_ALARM"); 
     Intent intent1 = new Intent(ctx,FirstQuoteAlarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, intent1, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pi); 
     Log.i("call2","msg"); 


} 
+0

Tại sao bạn cập nhật sau mỗi 10 phút? điều này là khá pin không thân thiện – Dediqated

Trả lời

0

Sử dụng ScheduledExecutorService. Điều đó thường cho kết quả tốt hơn.

Nó có nghĩa là để lặp lại hành động trong nền mỗi như vậy và vì vậy phút. Bắt đầu với sự chậm trễ và nhiều hơn nữa. Kiểm tra: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

Dưới đây là một lớp học với một phương pháp mà thiết lập một ScheduledExecutorService để phát ra tiếng bíp mỗi mười giây cho một giờ:

import static java.util.concurrent.TimeUnit.*; 
class BeeperControl { 
private final ScheduledExecutorService scheduler = 
Executors.newScheduledThreadPool(1); 

public void beepForAnHour() { 
final Runnable beeper = new Runnable() { 
    public void run() { System.out.println("beep"); 
}; 
final ScheduledFuture beeperHandle = 
    scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); 
scheduler.schedule(new Runnable() { 
    public void run() { beeperHandle.cancel(true); } 
}, 60 * 60, SECONDS); 
} 
}} 
1

Bạn nên sử dụng id thông báo khác nhau như sau mã bạn sử dụng

mNotificationManager.notify(i, notification); 

Và cũng tăng khi thời gian của bạn

Notification notification = new Notification(icon, contentTitle, when); 
Các vấn đề liên quan