我有一个应用程序使用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);
答案 0 :(得分:0)
最好在完成服务后重新安排,而不是依赖长期生活警报管理员电话。此外,您应该考虑使用唤醒广播接收器而不是自己管理waklock。