package com.example.lbstest2;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient;
private TextView positionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(getApplicationContext());
setContentView(R.layout.activity_main);
mLocationClient.registerLocationListener(new MyLocationListener());
positionText = (TextView) findViewById(R.id.position_text_view);
List<String> permissionList = new ArrayList<>();
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!permissionList.isEmpty())
{
String[] permissions = permissionList.toArray(new String [permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else
{
requestLocation();
}
}
private void requestLocation()
{
initLocation();
mLocationClient.start();
}
private void initLocation()
{
LocationClientOption option = new LocationClientOption();
option.setScanSpan(5000);
option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
mLocationClient.setLocOption(option);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode)
{
case 1:
if(grantResults.length > 0)
{
for(int result : grantResults)
{
if(result != PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this,"没有权限将无法使用本程序", Toast.LENGTH_SHORT).show();
}
finish();
return;
}
requestLocation();
}else
{
Toast.makeText(this,"发生未知错误,请尝试重新安装本程序",Toast.LENGTH_SHORT).show();
finish();
}
default:
break;
}
}
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPosition = new StringBuilder();
try {
currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("定位方式:");
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
currentPosition.append("GPS");
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
currentPosition.append("网络");
} else {
currentPosition.append("未知错误");
}
positionText.setText(currentPosition);
Toast.makeText(MainActivity.this,"定位成功\n"+currentPosition,Toast.LENGTH_SHORT).show();
}catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
}
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient;
private TextView positionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(getApplicationContext());
setContentView(R.layout.activity_main);
mLocationClient.registerLocationListener(new MyLocationListener());
positionText = (TextView) findViewById(R.id.position_text_view);
List<String> permissionList = new ArrayList<>();
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!permissionList.isEmpty())
{
String[] permissions = permissionList.toArray(new String [permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else
{
requestLocation();
}
}
private void requestLocation()
{
initLocation();
mLocationClient.start();
}
private void initLocation()
{
LocationClientOption option = new LocationClientOption();
option.setScanSpan(5000);
option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
mLocationClient.setLocOption(option);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode)
{
case 1:
if(grantResults.length > 0)
{
for(int result : grantResults)
{
if(result != PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this,"没有权限将无法使用本程序", Toast.LENGTH_SHORT).show();
}
finish();
return;
}
requestLocation();
}else
{
Toast.makeText(this,"发生未知错误,请尝试重新安装本程序",Toast.LENGTH_SHORT).show();
finish();
}
default:
break;
}
}
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPosition = new StringBuilder();
try {
currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("定位方式:");
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
currentPosition.append("GPS");
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
currentPosition.append("网络");
} else {
currentPosition.append("未知错误");
}
positionText.setText(currentPosition);
Toast.makeText(MainActivity.this,"定位成功\n"+currentPosition,Toast.LENGTH_SHORT).show();
}catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
}