Java 后台服务在android的onCreate()和onResume()中随机停止工作
我正在开发android应用程序,需要启动定位服务。如果我按下后退按钮/主页按钮,或者即使我按下主页按钮扫描应用程序,我所需要做的就是确保服务能够工作,无论它是否在任何活动中。我的定位服务在一段时间后停止工作,就像我设定了1分钟的计时,但它会在2-3分钟后调用它Java 后台服务在android的onCreate()和onResume()中随机停止工作,java,android,service,background-service,Java,Android,Service,Background Service,我正在开发android应用程序,需要启动定位服务。如果我按下后退按钮/主页按钮,或者即使我按下主页按钮扫描应用程序,我所需要做的就是确保服务能够工作,无论它是否在任何活动中。我的定位服务在一段时间后停止工作,就像我设定了1分钟的计时,但它会在2-3分钟后调用它 private static final LocationRequest REQUEST = LocationRequest.create() .setInterval(1000 * 60 * 1) // 30
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(1000 * 60 * 1) // 30 minutes seconds
.setFastestInterval(1000 * 60 * 1) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
下面给出了调用位置服务类和我正在运行的调用该服务的主类的代码。请在上面描述的场景中帮助我,我想在后台运行该服务:当按下后退按钮时,按下主页按钮,通过按下主页按钮删除应用程序
public class GPSLoggerService extends Service {
private LocationManager lm;
private static long minTimeMillis = 2000;
private static long minDistanceMeters = 0;
private static float minAccuracyMeters = 35;
private static boolean showingDebugToast = false;
MyLocationTracker locationTracker;
private static final String tag = "MUrgency GPS Logger";
/** Called when the activity is first created. */
private void startLoggerService() {
if (locationTracker != null)
return;
locationTracker = new MyLocationTracker(this) {
@Override
public void onLocationFound(Location location) {
Constants.sMY_LOCATION = location;
float a = (float) location.getLatitude();
float b = (float) location.getLongitude();
SharedPreferences prefs = getSharedPreferences("locationPref", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("latitudeFloat", a);
editor.putFloat("longitudeFloat", b);
editor.commit();
if (minutes > 5){
shouldSync = true;
}
}
};
}
private void shutdownLoggerService() {
}
}
@Override
public void onCreate() {
super.onCreate();
startLoggerService();
}
@Override
public void onDestroy() {
super.onDestroy();
shutdownLoggerService();
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/**
* Class for clients to access. Because we know this service always runs in
* the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
GPSLoggerService getService() {
return GPSLoggerService.this;
}
}
}
我在onCreate()调用服务的主类
从我的观点来看,这是一个正常的过程,当应用程序进入OnPause方法时,它开始在后台工作,然后你需要一个程序来执行你想要的类和函数 如果这是您第一次使用并行编程,我认为您需要花一点时间来搜索有关这方面的信息。使用后台进程是一种令人惊奇的形式。实际上,这是普通安卓程序员和专业安卓程序员之间的区别(除其他外),因为后台进程可以使用设备的所有功能
如果我帮了你,告诉我,好的编程 当用户按下后退按钮时,将调用您的
活动的onDestroy()
-方法。请参阅Android生命周期回调。你也可以看看
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainlanding);
startService(new Intent(this, GPSLoggerService.class));
}
@Override
protected void onDestroy() {
sActivityMain = null;
super.onDestroy();
stopLocationService();
}