Android GPS定位

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class GPSActivity extends Activity {
    //声明位置管理对象
     private LocationManager locationManager;
     //声明位置监听对象
     private LocationListener locationListener;
     //声明字符串变量
     String locationprovider;
     //声明显示文本视图组建
     private TextView textview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获得文本视图
        textview = (TextView)this.findViewById(R.id.textView1);
        try{
            //新建Criteria类
            Criteria locationcriteria = new Criteria();
            //设置精确精度
            locationcriteria.setAccuracy(Criteria.ACCURACY_FINE);
            //不提供海拔高度信息
            locationcriteria.setAltitudeRequired(false);
            //不提供方向信息
            locationcriteria.setBearingRequired(false);
            //允许运营商计费
            locationcriteria.setCostAllowed(true);
            //设置电池消耗为低耗费
            locationcriteria.setPowerRequirement(Criteria.POWER_LOW);
            //使用getSystemService()方法获得位置管理器对象
            locationManager
            =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            //locationManager.setTestProviderEnabled("gps", true);
            Toast.makeText(GPSActivity.this, "getSystemService", Toast.LENGTH_SHORT).show();
            //检查gps功能开启
            if(checkgps()){
                locationprovider 
                =locationManager.getBestProvider(locationcriteria, true);
                Log.d("provider", locationprovider);
                //注册位置监听器
                locationListener = new MyLocationListener();
                locationManager.requestLocationUpdates(locationprovider, 1000, 0,locationListener);
            }
        }catch(Exception e){
            Toast.makeText(GPSActivity.this, "异常错误"+e.toString(),Toast.LENGTH_LONG).show();
        }

    }
    private class MyLocationListener implements LocationListener{
         /**
          * 若位置发生变化,onLocationChanged方法被调用
          */
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.i("位置发生变化", "Invoke");
            if(location != null){
                //获得经度
                String latitude = Double.toString(location.getLatitude());//经度

                //获得纬度

               String longitude = Double.toString(location.getLongitude());//纬度
                //在文本框中显示
               textview = (TextView)GPSActivity.this.findViewById(R.id.textView1);
                textview.setText("经度:"+longitude+"纬度"+latitude);
            }
            //locationManager.removeUpdates(this);
            //locationManager.setTestProviderEnabled("gps", true);
        }
         //若屏蔽提供商,该方法被调用

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Log.i("屏蔽提供商", "Invode");
        }
        //若激活提供商,该方法被调用
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            Log.i("激活提供商", "Invode");
        }
       //若状态发生变化,该方法被调用
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
            Log.i("状态发生变化", "Invode");
        }

    }
    private boolean checkgps(){
        boolean providerEnabled
         = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //若被激活,则返回真值
        if(providerEnabled ==true){
            Toast.makeText(this, "Gps模块活动正常", Toast.LENGTH_SHORT).show();
            return true;
        }
        else{
            Toast.makeText(this, "请开启GPS", Toast.LENGTH_SHORT);
            return false;
        }

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.antking.gps"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GPSActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

标签: isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:iOS隐藏键盘的代码

下一篇:iOS从远程地址获取图片并修改尺寸