Android GPS定位
发布时间:2020-05-24 14:56:11 所属栏目:Java 来源:互联网
导读:Android GPS定位
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 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,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;
}
}
}
<?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>
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java Spring MVC 上传下载文件配置及controller方法详解
- javafx-2 – JavaFX 2.2 FXML Validated TextField
- Java统计英文句子中出现次数最多的单词并计算出现次数的方法
- 如何使用SAX XML Schema Validator的验证消息进行内部化?
- 多线程 – 在单独的TThread块GUI线程中的操作
- java – 在JSTL参数中使用变量
- JAVA反射机制中getClass和class对比分析
- java – JTDS驱动程序是否已过时?
- java – 有没有办法强制Checkstyle忽略源代码中的特定警告?
- MyBatis Excutor 拦截器的巧妙用法
