gpt4 book ai didi

android - 以编程方式打开/关闭移动数据和 GPS - Android 5.0 以上

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:58 24 4
gpt4 key购买 nike

我想实现启用/禁用移动数据和 GPS 设置。我搜索了启用/禁用移动数据和 GPS 设置的可用 Android API,以下是我的发现。

启用/禁用移动数据 –1. 可以在 <5.0 Android 操作系统中执行启用/禁用。2. 从 5.0+ Android 操作系统开始,非系统应用程序尚无法启用/禁用移动数据。 从 5.0 或更高版本开始,我们在执行相同操作时会遇到此异常——第三方应用程序不使用该异常。Caused by: java.lang.SecurityException: 用户 10314 和当前进程都没有 android.permission.MODIFY_PHONE_STATE

是否有任何可能/可用的解决方案可用?

GPS 设置 -1. 我们能够以编程方式打开位置,但尚未找到禁用它的方法(不包括 Intent 方法)。

任何人都知道如何在不通过 Intent 移动到设置的情况下禁用 GPS(以编程方式定位)。

提前致谢。

最佳答案

我们正在使用 SettingsApi

更改 GPS 设置而不移动到设置屏幕

要检查 GPS 是打开还是关闭,您必须像下面这样检查,

public class GPSActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static String TAG = "GPSActivity";

// Required for setting API
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
GoogleApiClient googleApiClient;

private Button mBtnGPS;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);

mBtnGPS = (Button) findViewById(R.id.btnGPS);

mBtnGPS.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnGPS:
// Check GPS
checkGps();
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
googleApiClient = null;
checkGps();
}
}

public void checkGps() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(GPSActivity.this)
.addApiIfAvailable(LocationServices.API)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
googleApiClient.connect();

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);

builder.setAlwaysShow(true); // this is the key ingredient

PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result
.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i("GPS", "SUCCESS");
//getFbLogin();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i("GPS", "RESOLUTION_REQUIRED");
// Location settings are not satisfied. But could be
// fixed by showing the user
// a dialog.
try {
// Show the dialog by calling
// startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(GPSActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("GPS", "SETTINGS_CHANGE_UNAVAILABLE");
// Location settings are not satisfied. However, we have
// no way to fix the
// settings so we won't show the dialog.
break;
case LocationSettingsStatusCodes.CANCELED:
Log.i("GPS", "CANCELED");
break;
}
}
});
}
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

这是 Documentation

关于android - 以编程方式打开/关闭移动数据和 GPS - Android 5.0 以上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com