0
我有一個應用程序使用AlarmManager來每5分鐘反覆調用一次IntentService。它似乎可以在數小時甚至數天內正常工作,但最終可以停止。AlarmManager和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);
我已編輯帖子。如果我在服務完成時包含該代碼,然後重新啓動它,那會起作用嗎?謝謝。 – turtleboy
你爲什麼要阻止它?我只是將代碼添加到onHandleIntent()中,該代碼在最後調用void android.app.AlarmManager.set(int type,long triggerAtMillis,PendingIntent操作)。 – ekawas
好吧,我以爲我應該在啓動一個新的鬧鐘之前取消舊鬧鐘? – turtleboy