Android:意向服务
Android: Intent Service
我试图通过在 intentservice class 中实现 Fused location provider api 来获取位置,同时我在同一个服务中启动一个 newtimertask。
所以当调用 intentservice 时,timertask 启动并且 googleapi客户端连接以获取位置。
我希望如果位置不可用,我的定时器任务会在接下来的 60 秒内断开 googleapi 客户端。
但这不起作用...如果它没有获取位置...意图服务继续 运行 或 fusedlocatinprovider 继续寻找 location.so 我必须停止这些吗?
package com.example.rj.intentlocationapi;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class BackgrndService extends IntentService implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
LocationManager mlocationmanger;
int bool = 0;
CountDownTimer cnt;
public BackgrndService() {
super("BackgrndService");
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
return false;
}
}
@Override
protected void onHandleIntent(Intent intent) {
if (!isGooglePlayServicesAvailable()) {
stopLocationService();
}
mlocationmanger = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
cnt = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.w("haha", "going to finish it");
stopLocationService();
}
}.start();
mGoogleApiClient.registerConnectionCallbacks(this);
mGoogleApiClient.connect();
Log.w("haha", "blocked to finish it");
}
public void onConnected(Bundle bundle) {
Log.w("hello", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.w("hhaahaa", "Connection suspended stop here ");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.w("hhaahaa", "Connection failed stop here ");
}
@Override
public void onLocationChanged(Location location) {
Log.w("hello", "Firing onLocationChanged..............................................");
mCurrentLocation = location;
if (mCurrentLocation != null) {
Log.w("hi", mCurrentLocation.getLatitude() + " " + mCurrentLocation.getLongitude());
}
stopLocationService();
}
public void stopLocationService() {
Log.w("haha", "killing begins");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
cnt.cancel();
Intent alarm = new Intent(this, BackgrndService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarm, 0);
if (bool == 0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 1000 * 100, pendingIntent);
bool = 1;
}
}
}
如果在 onHandleIntent() 运行 期间没有向其发送更多命令,IntentService 在 onHandleIntent() 结束时自动停止。因此,您不必自己手动停止 IntentService。
要么将 Intent-Service 转移或更改为 Service 并使用 stopSelf() 方法停止 service.This 方法停止服务。
我试图通过在 intentservice class 中实现 Fused location provider api 来获取位置,同时我在同一个服务中启动一个 newtimertask。
所以当调用 intentservice 时,timertask 启动并且 googleapi客户端连接以获取位置。
我希望如果位置不可用,我的定时器任务会在接下来的 60 秒内断开 googleapi 客户端。
但这不起作用...如果它没有获取位置...意图服务继续 运行 或 fusedlocatinprovider 继续寻找 location.so 我必须停止这些吗?
package com.example.rj.intentlocationapi;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class BackgrndService extends IntentService implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
LocationManager mlocationmanger;
int bool = 0;
CountDownTimer cnt;
public BackgrndService() {
super("BackgrndService");
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
return false;
}
}
@Override
protected void onHandleIntent(Intent intent) {
if (!isGooglePlayServicesAvailable()) {
stopLocationService();
}
mlocationmanger = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
cnt = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.w("haha", "going to finish it");
stopLocationService();
}
}.start();
mGoogleApiClient.registerConnectionCallbacks(this);
mGoogleApiClient.connect();
Log.w("haha", "blocked to finish it");
}
public void onConnected(Bundle bundle) {
Log.w("hello", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.w("hhaahaa", "Connection suspended stop here ");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.w("hhaahaa", "Connection failed stop here ");
}
@Override
public void onLocationChanged(Location location) {
Log.w("hello", "Firing onLocationChanged..............................................");
mCurrentLocation = location;
if (mCurrentLocation != null) {
Log.w("hi", mCurrentLocation.getLatitude() + " " + mCurrentLocation.getLongitude());
}
stopLocationService();
}
public void stopLocationService() {
Log.w("haha", "killing begins");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
cnt.cancel();
Intent alarm = new Intent(this, BackgrndService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarm, 0);
if (bool == 0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 1000 * 100, pendingIntent);
bool = 1;
}
} }
如果在 onHandleIntent() 运行 期间没有向其发送更多命令,IntentService 在 onHandleIntent() 结束时自动停止。因此,您不必自己手动停止 IntentService。
要么将 Intent-Service 转移或更改为 Service 并使用 stopSelf() 方法停止 service.This 方法停止服务。