java - 在后台更新位置并检查 LocationSettings

标签 java android service location fusedlocationproviderapi

我想每隔几分钟在后台更新一次用户的位置。
我使用了这个示例代码 googlesamples/android-play-location
但将其更改为与 Service

一起使用
public class MyService extends Service implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener, ResultCallback<LocationSettingsResult>

但我无法检查位置设置

    protected void checkLocationSettings() {
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(
                        mGoogleApiClient,
                        mLocationSettingsRequest
                );
        result.setResultCallback(this);
    }

    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                        "upgrade location settings ");

                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        Log.i(TAG, "User agreed to make required location settings changes.");
                        startLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i(TAG, "User chose not to make required location settings changes.");
                        break;
                }
                break;
        }
    }

因为这个方法需要一个Activity

status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);

如果我将之前的代码放在 MainActvity 中,我将不会有对 mGoogleApiClient 和 mLocationSettingsRequest 的引用

LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, mLocationSettingsRequest);

即使 Activity 被破坏,我也想升级并在后台保存位置。
使用服务是正确的做法吗?
如何检查位置设置?

[编辑]
我尝试使用 PendingIntentIntentService

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                    mLocationRequest, mPendingIntent);

This method is suited for the background use cases, more specifically for receiving location updates, even when the app has been killed by the system.

与带有 ServiceLocationListner 的版本相比有什么区别?

最佳答案

我找到了解决问题的方法。
为了访问主 Activity ,我使用了方法

MainActivity.getInstance()

主 Activity

public class MainActivity extends AppCompatActivity {
    private static MainActivity instance;

    public static MainActivity getInstance() {
        Log.i("MainActivity", "getInstance");
        return instance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Instance of the MainActivity
        instance = this;
    }
    ...
   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "onActivityResult");
    switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Log.i(TAG, "User agreed to make required location settings changes.");
                    startService(intent);
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i(TAG, "User chose not to make required location settings changes.");
                    break;
            }
            break;
        }
    }
}

服务

@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
    final Status status = locationSettingsResult.getStatus();
    switch (status.getStatusCode()) {
        case LocationSettingsStatusCodes.SUCCESS:
            // All location settings are satisfied. The client can
            // initialize location requests here.
            Log.i(TAG, "All location settings are satisfied.");
            startLocationUpdates();
            break;
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            // Location settings are not satisfied, but this can be fixed
            // by showing the user a dialog.
            Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to " +
                    "upgrade location settings ");
            try {
                // Show the dialog by calling startResolutionForResult(),
                // and check the result in onActivityResult().
                status.startResolutionForResult(MainActivity.getInstance(), REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                // Ignore the error.
                Log.i(TAG, "PendingIntent unable to execute request.");
            }
            break;
        // The status will never be SETTINGS_CHANGE_UNAVAILABLE if use builder.setAlwaysShow(true);
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            // Location settings are not satisfied. However, we have no way
            // to fix the settings so we won't show the dialog.
            Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                    "not created.");
            break;
    }
}

关于java - 在后台更新位置并检查 LocationSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597770/

相关文章:

java - 单击鼠标关闭 JFrame

bash - Fedora 17 上为 "service asterisk start"

java - 未经检查将 java.io.Serializable 转换为 java.util.ArrayList

android - Fragment 中的 ViewPager 在 transaction.remove() 之后消失(维护实例?)

java - 电子邮件应用程序无法在真实设备上运行

delphi - 服务无法访问文件夹

android - 我应该使用什么?服务 ?异步任务?还有别的吗?

java - Firebase 检索父项和子项的数据并将其分配给类

java - 异常实用程序类必须注意的事项

java - 404 错误配置与预编译的 jsp 不工作