上下文:我試圖通過AlarmManager實現一個服務調用第二個服務(第一個子類)。爲此,我需要創建一個廣播接收器作爲內部類,如此處推薦的(Service and a BroadCastReceiver)。但是,看起來第一個服務永遠不會成功地調用第二個服務,或者第二個服務沒有收到該呼叫。在服務中調用AlarmManager
我環顧四周,發現其他人有類似的問題,從未解決:AlarmManager from service。
在第一個服務調用第二的代碼很簡單:
private final long ONE_MINUTE = 60*1000*1;
Context theContext = getBaseContext();
AlarmManager theService = (AlarmManager) theContext
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(theContext, LocalWordSubClass.class);
PendingIntent thePending = PendingIntent.getBroadcast(theContext, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
//call this after two minutes
theService.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+TWO_MINUTES, thePending);
我的第二項服務是:
public class LocalWordSubClass extends LocalWordService {
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(checkIfGooglePlay() && checkTime()) {
getPostLocation();
}
mLocationClient.disconnect(); //connected in the first service class
stopSelf();
}
};
}
我的清單與識別的第二服務:
<service
android:name=".LocalWordSubClass"
android:label="LocalWordSubClass" >
</service>
問題:我沒有實現一個方法嗎?或者我是否需要將廣播接收器添加到清單中?對於第二班爲什麼沒有迴應,我有點困惑。
**更新:**感謝CommonWare的多項建議,我能夠在連接LocationClient和獲取其最後位置之間達到這一分鐘的時間。但是,當MainActivity的AlarmManager的setRepeating方法調用時,該服務不再重複運行。
這是代碼(即成功給locationClient時間來連接)
if(mLocationClient==null) {
pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
initalizeLocks();
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
Context theContext = getBaseContext();
AlarmManager theService = (AlarmManager) theContext
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(LocalWordService.this, this.getClass());
PendingIntent thePending = PendingIntent.getService(this, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
//call this after two minutes
theService.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONE_MINUTE, thePending);
} else {
getPostLocation();
wl.release();
mLocationClient = null;
stopSelf();
}
在MainActivity的AlarmManager是:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, LocalWordService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.
setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), EVERY_TWO_MINUTES, pintent);
@CommmonsWare,我仍然不確定如何實現您的建議。即使我改變的PendingIntent到的getService(看起來像這樣:意向I =新意圖(theContext,LocalWordService.class); \t \t的PendingIntent thePending = PendingIntent.getService(theContext,0,I, \t \t \t \t PendingIntent.FLAG_CANCEL_CURRENT);)我不明白服務如何接收這個調用,而且我怎樣才能避免一個服務總是在運行的遞歸 - 死亡陷阱。 – NumenorForLife
爲了更清楚,我不確定如何在兩分鐘時間內識別服務,因爲沒有onReceive()方法。對於該方法或類似的方法,我需要使用該位置,斷開客戶端,釋放喚醒鎖,然後終止服務。 – NumenorForLife
@ jsc123:「我不明白服務如何接收該調用」 - 它將通過'onStartCommand()'調用,您可以通過閱讀'PendingIntent'上的getService()文檔來得知: http://goo.gl/f3hW3 – CommonsWare