0

我有一个应用程序,它使用 AlarmManager 每 5 分钟重复调用一次 IntentService。它似乎可以正常工作数小时甚至数天,但最终它会停止。

为什么是这样?我可以做些什么来检查它是否已被杀死并重新启动它?

谢谢。

// get a Calendar object with current time
             Calendar cal = Calendar.getInstance();
             // add 5 minutes to the calendar object
             cal.add(Calendar.MINUTE, 1);
             Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
             intent.putExtra("alarm_message", "sending outstanding transactions");
             // In reality, you would want to have a static variable for the request code instead of 192837
             PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

             // Get the AlarmManager service
             AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
             //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
             //86400000 = 24 hours
             //43200000 = 12 hours
             //3600000 = 1hr
             //1800000 = 30 mins
            // 600000 = 10 mins
             //300000 = 5 mins

             am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300000 , sender);

.

public class AlarmReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {





     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
     NfcScannerApplication.wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "com.something.alarm");
     if(!NfcScannerApplication.wl.isHeld()){
         Log.e("AlarmReceiver", "aquiring wake lock in alarmmanager");
         NfcScannerApplication.wl.acquire(15000);

     KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
     km.newKeyguardLock("AlarmReceiver").disableKeyguard();
    }

   try {

     Bundle bundle = intent.getExtras();
     String message = bundle.getString("alarm_message");
    // Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
     myIntent.setAction("com.carefreegroup.rr3.startatboot.MyService");
     context.startService(myIntent);

    } catch (Exception e) {
     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
     e.printStackTrace();

    }
 }

}

.

public class SendOutstandingTransactions extends IntentService {

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;
    Cursor c;
    //LocationManager             mlocManager;
    //LocationListener            mlocListener;
    SharedPreferences appSharedPrefs;
    Editor  prefsEditor;
    String companyID;

    @Override
    public void onCreate() {
        super.onCreate();
        nfcscannerapplication = (NfcScannerApplication)getApplication();
        //mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(SendOutstandingTransactions.this.getApplicationContext());
        prefsEditor = appSharedPrefs.edit();

    }





    @Override
    protected void onHandleIntent(Intent intent) {


        //do something

}

[编辑]

            Intent intentstop = new Intent(NfcscannerActivity.this, AlarmReceiver.class);
             PendingIntent senderstop = PendingIntent.getBroadcast(NfcscannerActivity.this, 192837, intentstop, 0);
             AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);
             alarmManagerstop.cancel(senderstop);
4

1 回答 1

0

在完成服务后重新安排时间可能会更好,而不是依赖于长期存在的警报管理器呼叫。此外,您应该考虑使用唤醒广播接收器,而不是自己管理唤醒锁。

于 2013-11-19T16:03:18.137 回答