我正在开发一个在android中的跟踪应用程序。Android:IntentService突然停止
虽然我测试我发现一个问题的应用程序:
我使用AlarmManager开始调用的IntentService一个WakefulBroadcast,该OnHandleIntent方法里面我打电话从FusedLocation requestLocationUpdates(),并启动处理程序/ CountDownTimer删除LocationUpdates
问题是,有时服务突然停止,没有警告或解释。
和日志停止显示locationUpdate工作或CountDownTimer执行
有人可以帮助我知道为什么我的IntentService突然停止?还是有另一种方式来保持服务执行/活着,而接收locationUpdates?
的ServiceManager:
public void StartExecuteSubscriptionService(long nextTimeMillis){
Intent intent = new Intent(contextWeakReference.get(),ExecSubscriptionWakefulBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(contextWeakReference.get(),
Constants.EXEC_SUBSCRIPTIONS_SERVICE_REQUESTCODE,
intent, 0);
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager am = (AlarmManager) contextWeakReference.get().getSystemService(Context.ALARM_SERVICE);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
am.set(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
am.setExact(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTimeInmillis, pendingIntent);
}
}
WakefulBroadcast:
public class ExecSubscriptionWakefulBroadcast extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ExecSubscriptionsService.class);
// Start the service, keeping the device awake while it is launching.
CatchExceptionsUtils.saveException(context,new Exception("Ejecutando ExecSubscriptionWakefulBroadcast "+ TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
startWakefulService(context, service);
}
}
ExecSuscriptionService:
@Override
protected void onHandleIntent(@Nullable Intent intent) {
CatchExceptionsUtils.saveException(this,new Exception("Ejecutando ExecSubscriptionService "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
try{
LocationUpdateRequester locationUpdateRequester = new LocationUpdateRequester(this.getApplicationContext(),intent);
locationUpdateRequester.requestLocationUpdates();
}catch (Exception e){
CatchExceptionsUtils.saveException(this,e);
}
}
LocationUpdateRequester:
public void requestLocationUpdates() throws SecurityException {
try{
googleApiClient.connect();
}catch (Exception e){
CatchExceptionsUtils.saveException(contextWeakReference.get(),e);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) throws SecurityException {
try{
CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Conectado a GoogleServices "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Iniciando la busqueda "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, TelephonyUtils.createLocationRequest(),listener);
countDownTimer = new CountDownTimer(Constants.TIMEOUT_GPS,20000) {
@Override
public void onTick(long millisUntilFinished) { CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Servicio "+ejecucionId+" encendido y ejecutando"));
}
@Override
public void onFinish() {
CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Ejecutando CountDown "+TelephonyUtils.getSystemDate("yyyy-MM-dd HH:mm:ss")));
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,listener);
googleApiClient.disconnect();
releaseWake();
}
}.start();
}catch (Exception e){
CatchExceptionsUtils.saveException(contextWeakReference.get(),e);
releaseWake();
}
}
监听器:
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(location!=null){
CatchExceptionsUtils.saveException(contextWeakReference.get(),new Exception("Guardando registro; " +location.getAccuracy()+" "+location.getLatitude()+" "+location.getLongitude()+" "+TelephonyUtils.getSystemDate("yyyy/MM/dd HH:mm:ss")+" "+ejecucionId));
locationList.add(location);
}
}
};
18 Ejecutando ExecSubscriptionWakefulBroadcast 2017年5月29日15时11分14秒15 2017年5月29日:11:13.000
18 Ejecutando ExecSubscriptionService 2017 -05-29 15:11:15 2017-05-29 15:11:13.000
18保存取消回复:0 minutos 2017-05-29 15:11:14.000
18 Conectando一个GoogleServices 2017年5月29日15时11分15秒15 2017年5月29日:11:14.000
18 Conectado一个GoogleServices 2017年5月29日15时11分15秒2017-05- 29 15:11:14.000
18 Iniciando LA busqueda 2017年5月29日15时11分16秒15 2017年5月29日:11:14.000
18 busqueda iniciada 2017年5月29日15:11: 16 2017-05-29 15:11:14.000
18服务1496088675756上一个下一个下一个2017-05-29 15:11:14.000
18 Guardando registro; 2017/05/29 15:11:17 1496088675756 2017-05-29 15:11:15.000
18 Guardando registro; 2017/05/29 15:11:32 1496088675756 2017-05-29 15:11:31。000
18 SERVICIO 1496088675756 encendidoýejecutando 2017年5月29日15:11:34.000
18 Ejecutando ExecSubscriptionService 2017年5月29日15时26分十七秒2017年5月29日15点26分: 16.000
这工作就像一个魅力。非常感谢。我认为处理程序会做到保持“一次性”的诀窍,我将我的IntentService更改为一个服务,并完美运行:D –