以编程方式打开GPS,如Ola Cabs Android应用程序 [英] Turn GPS on programmatically like Ola Cabs Android app

查看:146
本文介绍了以编程方式打开GPS,如Ola Cabs Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个应用程序,需要集成GPS位置。我想以编程方式切换GPS。条件是:我不想将用户发送到设置面板以启用它。我想强制启用它或单个提示符将起作用(类似于Ola Cabs Android应用程序)。在这个网站上有很多这样的问题,但是每个人都在寻找类似于Ola Cabs应用程序的功能。所以我已经开始这个线程,以便我们所有人都可以清楚。

I am working on an application and need to integrate GPS location. I want to switch the GPS on programmatically. The condition is: I don't want to send the user to the Setting panel to enable it. I want to enable it forcefully or a single prompt will work (similar to the Ola Cabs Android app). A lot of questions are for this on this site but everyone is looking for the similar functionality like Ola Cabs app. So I have started this thread so that it can be clear for all of us.

推荐答案

这是我的100%工作代码,首先添加这个依赖 compile'c​​om.google.android。 gms:play-services:9.2.1'到您的build.gradle(模块:应用程序)

Here is my 100% working code first add this dependencie compile 'com.google.android.gms:play-services:9.2.1' to your build.gradle(Module: app)

之后创建名称为 StartLocationAlert.java 并复制此代码并粘贴到此文件中。

After that create class with name StartLocationAlert.java and copy this code and paste in this file

import android.app.Activity;
import android.content.IntentSender;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.example.googlemappromt.MainActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;


/**
 * Created by Anirudh on 20/07/16.
 */
public class StartLocationAlert implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener  {
    Activity context;
    protected static final int REQUEST_CHECK_SETTINGS = 0x1;
    GoogleApiClient googleApiClient;
    public StartLocationAlert(Activity context) {
        this.context = context;
        googleApiClient = getInstance();
        if(googleApiClient != null){
            //googleApiClient.connect();
            settingsrequest();
            googleApiClient.connect();
        }
    }

    public  GoogleApiClient getInstance(){
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
        return mGoogleApiClient;
    }

    public void settingsrequest()
    {
        Log.e("settingsrequest","Comes");


        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:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                       // Log.e("Application","Button Clicked");
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                       // Log.e("Application","Button Clicked1");
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(context, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                            Log.e("Applicationsett",e.toString());
                        }
                        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.
                        //Log.e("Application","Button Clicked2");
                        Toast.makeText(context, "Location is Enabled", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {


    /*    MainActivity mm = new MainActivity();
        mm.requestLocationUpdates();*/
        Toast.makeText(context , "Connected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}



Usage of this above class is show below

 @Override
    protected void onResume() {
        super.onResume();
        Activity mContext = MainActivity.this //change this your activity name 
        StartLocationAlert startLocationAlert = new StartLocationAlert(mContext);

       requestLocationUpdates();

    }

不要忘记在 AndroidManifest.xml 文件,否则这可能无法正常工作

And don't forget to add below permissions in AndroidManifest.xml file or else this might not work properly

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

希望这可以帮到你..

Hope this might help you..

这篇关于以编程方式打开GPS,如Ola Cabs Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆