package com.example.administrator.locationtest;import android.app.Activity;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.content.ContextCompat;import android.widget.TextView;import android.widget.Toast;import org.w3c.dom.Text;import java.util.List;import java.util.jar.Manifest;/** * Created by Administrator on 2016/4/8. */public class MainActivity extends Activity{    private TextView positionTextView;    private LocationManager locationManager;    private String provider;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        positionTextView=(TextView)findViewById(R.id.position_text_view);        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);        if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
//此处的判定是主要问题,API23之后需要先判断之后才能调用locationManager中的方法
//包括这里的getLastKnewnLocation方法和requestLocationUpdates方法            Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);            if(location!=null){                showLocation(location);            }            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,locationListener);        }    }    @Override    protected void onDestroy() {        super.onDestroy();        if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
//此处同样需要判定之后才能调用removeUpdates方法            if(locationManager!=null){                locationManager.removeUpdates(locationListener);            }        }    }    LocationListener locationListener=new LocationListener() {        @Override        public void onLocationChanged(Location location) {            showLocation(location);        }        @Override        public void onStatusChanged(String provider, int status, Bundle extras) {        }        @Override        public void onProviderEnabled(String provider) {        }        @Override        public void onProviderDisabled(String provider) {        }    };    private void showLocation(Location location){        String currentPosition="latitude is"+location.getLatitude()+"\n"+"longitude is"+location.getLongitude();        positionTextView.setText(currentPosition);    }}


更多相关文章

  1. Android(安卓)的AsyncTask使用
  2. 分享一个Android日志记录的工具类
  3. Android(安卓)上实现微信微博上的应用语言切换
  4. 升级android studio3.0遇到的问题
  5. Android标题栏(titlebar)显示进度条
  6. Android中调用摄像头并实现对焦拍照
  7. android proguard 错误处理
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android源码中的单例模式
  2. android传感器sensor
  3. 【winows7+android-ndk-r9+Cygwin 】coco
  4. 调用Android系统“应用程序信息(Applicati
  5. android之requestWindowFeature详解
  6. android 设置应用程序 默认值
  7. JAVA/Android(安卓)读写文件,避免中文乱码
  8. Android性能测试之内存泄露以及GC机制浅
  9. cocos2dx 在mac下开发ios和android游戏
  10. Android(安卓)Studio打包apk