2012-05-21 29 views
13

Tôi cần bắt đầu hoạt động AlarmReceiver sau 10 giây (ví dụ). Tôi cần nó được kích hoạt mà không cần chạy ứng dụng. Nhưng liệu ứng dụng có chạy hay không AlarmReceiver không được gọi. Bất kỳ đề xuất?AlarmManager không hoạt động

Intent intent = new Intent(this, AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

//alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
              //+ (10 * 1000), pendingIntent); 
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show(); 
+0

bạn đã thử câu trả lời @Pratik D chưa? –

Trả lời

26
public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      String message = "Hellooo, alrm worked ----"; 
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
      Intent intent2 = new Intent(context, TripNotification.class); 
      intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(intent2); 
    } 

    public void setAlarm(Context context){ 
     Log.d("Carbon","Alrm SET !!"); 

     // get a Calendar object with current time 
     Calendar cal = Calendar.getInstance(); 
     // add 30 seconds to the calendar object 
     cal.add(Calendar.SECOND, 30); 
     Intent intent = new Intent(context, AlarmReceiver.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get the AlarmManager service 
     AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
    } 
} 

Đây là mã cuối cùng tôi quản lý để có được làm việc. Bạn cần phải thêm

<receiver android:process=":remote" android:name="AlarmReceiver"></receiver> 

ngay phía trên thẻ </application> trong tệp kê khai.

này sẽ thiết lập một báo động để kích hoạt trong vòng 30 giây sau khi gọi phương pháp SetAlarm()

+0

@Alex @ dinesh707 Trường hợp 'SetAlarm()' được gọi là? – Neo

+8

Hiệu chỉnh: Phần người nhận phải nằm trong thẻ ứng dụng! –

+1

@JamesCameron Bạn đã lưu người bạn đêm của mình .. – learner

6

Tính đến bây giờ, nó không thể bắt đầu báo động mà không cần chạy ứng dụng, bạn một lần phải chạy ứng dụng tương ứng của bạn để kích hoạt báo thức của bạn .. Đối với điều này .... !!

Trong ALARM_ACTIVITY của bạn:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

Intent intent = new Intent(ALARM_ACTIVITY.this,ALARM_RECEIVER.class); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(SetReminder.this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis() + 1000, pendingIntent); 

Trong ALARM_RECEIVER bạn:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

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

notification = new Notification(R.drawable.alarmicon, "charSequence", System.currentTimeMillis()); 

notification.setLatestEventInfo(context, "alarmTitle", "charSequence", pendingIntent); 

notification.flags |= Notification.FLAG_AUTO_CANCEL; 

notificationManager.notify(1, notification); 
-1

Ngoài ra, Ngoài việc trên, tôi nghĩ rằng các phương pháp trong AlarmActivity nên trong phương thức onCreate của hoạt động Launcher .. Trong trường hợp này, Alarm Activvity sẽ là hoạt động LAUNCHER của ứng dụng. điều này đã giải quyết được sự cố của tôi

3

Và nếu nó vẫn không hoạt động, hãy thoát khỏi phần android:process=":remote" có thể hữu ích. Đã làm việc cho tôi :)

Các vấn đề liên quan