Java 如何让应用程序在后台运行
我有一个基于android应用程序的位置,我想在应用程序最小化而不是销毁时获得用户位置更新 我尝试使用服务获取后台服务,但是,它不工作,当位置更改时,它不会打印日志消息 这就是我所尝试的: 从“我的地图”活动:Java 如何让应用程序在后台运行,java,android,service,location,background-process,Java,Android,Service,Location,Background Process,我有一个基于android应用程序的位置,我想在应用程序最小化而不是销毁时获得用户位置更新 我尝试使用服务获取后台服务,但是,它不工作,当位置更改时,它不会打印日志消息 这就是我所尝试的: 从“我的地图”活动: // onMapReady startService(new Intent(this, BackgroundLocation.class)); import android.app.Service; import android.content.Context; import and
// onMapReady
startService(new Intent(this, BackgroundLocation.class));
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class BackgroundLocation extends Service {
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
背景位置活动:
// onMapReady
startService(new Intent(this, BackgroundLocation.class));
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class BackgroundLocation extends Service {
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.util.Log;
公共类BackgroundLocation扩展服务{
私有静态最终字符串TAG=“BOOMBOOMTESTGPS”;
私有位置管理器mLocationManager=null;
专用静态最终int位置_间隔=1000;
专用静态最终浮动位置_距离=10f;
私有类LocationListener实现android.location.LocationListener{
位置mLastLocation;
公共位置侦听器(字符串提供程序){
Log.e(标记“LocationListener”+提供者);
mLastLocation=新位置(提供商);
}
@凌驾
已更改位置上的公共无效(位置){
Log.e(标签“onLocationChanged:+位置);
mLastLocation.set(位置);
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
Log.e(标记“onProviderDisabled:+提供程序”);
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
Log.e(标记“onProviderEnabled:+提供程序”);
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
Log.e(标记“onStatusChanged:+提供者”);
}
}
LocationListener[]mlLocationListeners=新LocationListener[]{
新LocationListener(LocationManager.GPS_提供程序),
新LocationListener(LocationManager.NETWORK\u提供程序)
};
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.e(标记“onStartCommand”);
super.onStartCommand(intent、flags、startId);
返回开始时间;
}
@凌驾
public void onCreate(){
Log.e(标记为“onCreate”);
初始化ElocationManager();
试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.NETWORK_提供程序、位置间隔、位置距离、,
mLocationListeners[1]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“网络提供商不存在”+ex.getMessage());
}
试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序、位置\u间隔、位置\u距离、,
mLocationListeners[0]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“gps提供程序不存在”+ex.getMessage());
}
}
@凌驾
公共空间{
Log.e(标签“onDestroy”);
super.ondestory();
if(mLocationManager!=null){
for(int i=0;i
当我退出应用程序并更改在emulator中的位置时,我不会在onLocationChanged中收到日志消息
谢谢你的帮助