我正在开发一个应该每10分钟将gps数据发送到服务器一次的应用程序。问题是当GPS打开时我杀了应用程序,GPS图标从状态栏消失(当GPS仍然打开时),数据不再发送。如果我对谷歌地图应用程序执行相同操作,则图标不会消失。 有人能解释我为什么会这样吗? 这是我使用的代码
AlarmManager
public class GpsDataService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
protected LocationRequest mLocationRequest;
/**
* Represents a geographical location.
*/
protected Location mCurrentLocation;
private String mPayLoad;
private GpsPojoBuilder mGpsPojoBuilder;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_NOT_STICKY;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
/**
* UPDATE_INTERVAL_IN_MILLISECONDS = 4 sec
* FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 2 sec
*/
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest
.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS)
.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
// The connection to Google Play services was lost for some reason. Call connect() to
// attempt to re-establish the connection.
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
checkForPermission();
}
private void checkForPermission() {
Location location;
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//permission denied
} else {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null)
handleLocation(location);
else
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}else {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(location != null)
handleLocation(location);
else
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onLocationChanged(Location location) {
handleLocation(location);
}
private void handleLocation(Location location){
mCurrentLocation = location;
Toast.makeText(this, "location foound: " + location.toString(), Toast.LENGTH_SHORT).show();
Thread thread = new Thread() {
@Override
public void run() {
try {
mGpsPojoBuilder = new GpsPojoBuilder(getApplicationContext());
mPayLoad = mGpsPojoBuilder
.getCheckInJson(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
sendJSON(mPayLoad, CHECKINS_URL);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
}
}
};
thread.start();
}
public void sendJSON(String payLoadData, String url_suffix) {
HttpConnectionCreator mHttpConncectionClass = new HttpConnectionCreator(payLoadData, url_suffix);
mHttpConncectionClass.connect();
}
/**
* Removes location updates from the FusedLocationApi.
*/
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
}
服务
@Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=700"
For %%A In (*.bat *.cmd *.txt) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
答案 0 :(得分:0)
根据android.developer,如果你想让你的服务保持活着,你需要使用START_STICKY。
int START_STICKY 从onStartCommand(Intent,int,int)返回的常量:如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),则将其保留在启动状态但不保留此状态交付意图。稍后系统将尝试重新创建服务。
在以下函数中:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_NOT_STICKY;
}
改为:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_STICKY;
}