您的位置:首页 > 移动开发 > Android开发

Android学习--位置信息经纬度获取+动态获取权限

2017-07-25 17:59 465 查看
        学习《第一行代码》代码笔记,此处学习的是第一版基于Eclipse的,但是Android Studio需要动态获取权限,所以有些部分不兼容部分。因为我把获取经纬度放在了点击事件里,所以处理监听位置改变的逻辑就不好插入进去,而且监听也需要实例化location并且获取权限。    关于回调函数,我也不是很明白,在调试的时候也没有进入到这个方法里去。暂且先保存在这里,作为一个学习记录,同时以后要用的时候直接拿下来更改。
package com.hrd.jc.test2;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView positionTextView;
private LocationManager locationManager;
private String provider;
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView = (TextView) findViewById(R.id.postion_text_view);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);//获取所有可用的位置提供器
//传入True表示只有启用的位置提供器才会被返回
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = locationManager.GPS_PROVIDER;
Log.i("权限log", "gps");
} else if (providerList.contains(locationManager.NETWORK_PROVIDER)) {
provider = locationManager.NETWORK_PROVIDER;
Log.i("权限log", "network");
} else {
Toast.makeText(this, "no location provider to use", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.btn) {
Log.i("权限log", "点击了按钮");
dosomething();
}
}

private void dosomething() {
//权限判断和获取
int permissionCheck = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("权限log", "没有权限");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
Log.i("权限log", "拒绝声明");
Toast.makeText(this, "u had reje
4000
cted the request", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
} else {//如果有权限,就直接进行事件处理
Log.i("权限log", "有权限");
getMylocation();
}
}

public void getMylocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
} else {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("权限log", "显示位置");
showLocation(location);//自定义方法
}
}
// locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
//监听位置变化,全局,但是我在此处把查询写成了点击事件,所以只在点击后响应,后续解决
//位置变化之后,触发show()方法。但是只有点击之后才有。,所以每次点击之后,满足两次变化监听之后,会tost两次
//如果要做一个实时位置信息,考虑设置一个循环,每多长时间进行一次查询,或者有变化了进行一次显示。,
}

public void OnRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,grantResults);
switch (requestCode) {
case 1:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.i("权限log", "回调");
Toast.makeText(MainActivity.this, "回调 ", Toast.LENGTH_SHORT).show();
getMylocation();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}

//位置变化监听
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {//更新当前的位置信息
showLocation(location);
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}

@Override
public void onProviderEnabled(String s) {
}

@Override
public void onProviderDisabled(String s) {
}
};

private void showLocation(Location location) {
String currentPosition = "latitude is" + location.getLatitude() + "\n" + "longitude is" + location.getLongitude();
positionTextView.setText(currentPosition);
Toast.makeText(this,"刷新了位置信息",Toast.LENGTH_SHORT).show();
}
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
//关闭程序将监听器移除
locationManager.removeUpdates(locationListener);
}
}

}


xml文件只有一个textView和一个button,id上面可看到,此处就不写了。

对于我的问题,如果有大神指点我将感激不尽,如果有同样的初学者,可以一起相互交流促进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: