后台服务在 onCreate() 和 onResume() 的 android 中随机停止工作

Background service stops working randomly in android in onCreate() and onResume()

我正在开发 android 需要启动定位服务的应用程序。我只需要确保该服务可以正常工作,无论它是否在任何 activity 上,如果我按下后退 button/home 按钮,或者即使我通过按下主页按钮扫描应用程序。我的定位服务在一段时间后停止工作,比如我将时间设置为 1 分钟,但它会在 2-3 分钟后调用它。

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);

下面给出了我的代码,用于调用位置服务 class 和主要 class,我正在 运行 调用该服务。请在上述场景中帮助我 运行 该服务在后台:当按下后退按钮、主页按钮时,通过按下主页按钮删除应用程序。

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;
    }
}

}

Main class 我在 onCreate() 调用服务的地方

@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();
    }

在我看来,这是一个正常的过程,当应用程序进入 OnPause 方法时,它开始在后台运行,然后你需要一个 background process 来执行你的 class 和功能你要。

如果这是您第一次使用并行编程,我认为您需要花一点时间来搜索有关这方面的信息。使用后台进程的形式非常棒。这确实是普通 android 程序员和专业 android 程序员(除其他外)之间的区别,因为后台进程可以使用您设备的所有功能。

如果我帮助了你,告诉我,好的编程!