بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
آیا این امکان وجود داره که gps رو از داخل برنامه روشن و خاموش کرد بدون ارجاع به صفحه تنظیمات؟؟؟؟؟
سلام
میخام از داخل برنامه gps کاربر و روشن خاموش کنم بدون اینکه به صفحه تنظیمات بره . آیا میشه اینکارو کرد و اگه کسی اینکارو کرده لطفا کمکم کنه .
ممنون
0
0
نه امکان نداره (8 سال پیش)
برای این سوال 1 پاسخ وجود دارد.
مشاهده پاسخ صحیح
پاسخ به سوال
Andronymous
8 سال پیش
+1
0
پاسخ صحیح
این که به طور کامل بدون این که کاربر ببینه بخوای GPS رو روشن کنی امکان نداره ولی یه روش مناسب که گوگل مپ هم ازش استفاده میکنه اینه:
اول باید به GoogelApiClient متصل بشی که برای اتصال هم پیشنهاد میکنم حتما به روز بودن Google play serivices رو روی دیوایس کاربر چک کنی...
public void connectToGoogleApiClient(){
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
displayLocationSettingsRequest();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// user google play services is out of date
}
})
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
}
بعد از اتصال، تابع زیر رو داخل لیسنر صدا زدم که اگر GPS خاموش باشه با یه دیالوگ شبیه گوگل مپ از کاربر اجازه روشن کردن رو میگیره و به صفحه تنظیمات نمیره .
public void displayLocationSettingsRequest() {
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);
//this is the key ingredient
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices
.SettingsApi
.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.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(
ActivityMain.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
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.
break;
}
}
});
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .