GPS定位那点事


<?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>

 实现

package com.android.antking.gps;

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;
    	}
    	
    }
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值