【问题标题】:GPS location after permission not working (SDK 23) android studio许可后的GPS位置不起作用(SDK 23)android studio
【发布时间】:2016-11-07 06:33:35
【问题描述】:

我已经创建了这个类,我需要将我的最后一个位置放在哪里?

public class DescriptionActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {

    TextView t;
    Button up;
    GoogleApiClient mGoogleApiClient;
    LocationManager locationManager;
    LocationListener locationListener;
    LocationServices mLastLocation;
    private static final int PERMS_REQUEST_CODE=123;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hr__description);
        Toolbar toolbar = (Toolbar) findViewById(R.id.hr_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        t = (TextView) findViewById(R.id.test_boaz);
        up=(Button)findViewById(R.id.upload);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
                .build();
        if(hasPermission())
        {
            getGPS();
        }
        else {
            requestPerms();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMS_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // All good!
                } else {
                   Log.d("bobbbbbbbbbbbbbbbbbb","boazboabzobaob");
                }

                break;
        }
    }

    private void getGPS() {
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }

    private boolean hasPermission(){
            int res=0;
            String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
            for(String params:permissions)
            {
                res=checkCallingOrSelfPermission(params);
                if(!(res==PackageManager.PERMISSION_GRANTED)) {
                    return false;
                }
            }
            return true;
        }

        private void requestPerms(){
            String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
             requestPermissions(permissions,PERMS_REQUEST_CODE);
                ActivityCompat.requestPermissions(this, permissions, PERMS_REQUEST_CODE);
            }

        }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {

        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
}

我尝试激活 GPS,但它总是要我检查权限 如何解决? 在互联网上查看了许多示例,我已成功获取对话框以激活 GPS 设备,但是当我尝试获取位置时出现问题

【问题讨论】:

  • 你能发布错误对话框吗?
  • 我没有任何错误,只是他要求我添加权限
  • 哪个权限?
  • 他是谁?
  • 您是否也在清单文件中添加了 ACCESS_COARSE_LOCATION 权限?

标签: android android-gps


【解决方案1】:

当您想要连接到所提供的 Google API 之一时 在 Google Play 服务库(例如 Google 登录、游戏或 Drive),你需要创建一个 GoogleApiClient 的实例(“Google API 客户端”)。Google API 客户端为所有用户提供了一个通用入口点 Google Play 服务并管理两者之间的网络连接 用户的设备和每项 Google 服务。

  • 自动管理您与 Google Play 服务的连接。
  • 对任何 Google 执行同步和异步 API 调用 播放服务。

  • 手动管理您与 Google Play 服务的连接 在极少数情况下需要这样做。

将此添加到清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.location.sample.basiclocationsample" >

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

在您的活动中添加此代码

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build()
 @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }

【讨论】:

  • 我需要在哪里插入此代码?在 OnCreate 方法上?
  • 我没有 Drive.API 它无法识别
  • 当我添加此代码时 public void onConnected(@Nullable Bundle bundle) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { } }
  • 我添加了此代码,但我仍然收到消息(调用请求的权限可能会被用户拒绝)
【解决方案2】:

在您的活动中的 onCreate 方法中

【讨论】:

  • 我没有 Drive.API 它无法识别
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2016-12-12
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多