AndroidManifest.xml

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

main.java

package com.mrsoft.mrgps.app.mr;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity {
private TextView tv;
private LocationManager lm;
private Criteria criteria;
private Location location;
private final static int MENU_ABOUT = Menu.FIRST;
private final static int MENU_EXIT = Menu.FIRST+1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (TextView)findViewById(R.id.tv);

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

if (!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS已关闭,请手动开启GPS后再试!", Toast.LENGTH_SHORT).show();
return;
}
else
{
Toast.makeText(this, "GPS定位中...", Toast.LENGTH_SHORT).show();
}

criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置精确度
criteria.setAltitudeRequired(true); // 设置请求海拔
criteria.setBearingRequired(true); // 设置请求方位
criteria.setCostAllowed(true); // 设置允许运营商收费
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

String provider = lm.getBestProvider(criteria, true);
location = lm.getLastKnownLocation(provider);
newLocalGPS(location);
// 监听1秒一次 忽略位置变化
lm.requestLocationUpdates(provider, 1*1000, 0, new locationListener());
}

class locationListener implements LocationListener
{

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
newLocalGPS(location);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
newLocalGPS(null);
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

}

private void newLocalGPS(Location location)
{
if (location!=null)
{
double latitude = location.getLatitude(); //精度
double longitude = location.getLongitude(); // 纬度
double speed = location.getSpeed(); // 速度
double altitude = location.getAltitude(); // 海拔
double bearing = location.getBearing(); // 方位
tv.setText("精度"+latitude+'\n'+
"纬度"+longitude+'\n'+
"速度"+speed+"m/s"+'\n'+
"海拔"+altitude+"m"+'\n'+
"方位"+bearing+'\n');
}
else
{
// 未获取地理信息位置
tv.setText("地理信息位置未知或正在获取地理信息位置中...");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_ABOUT, 1, "关于");
menu.add(0, MENU_EXIT, 2, "退出");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case MENU_ABOUT:
AlertDialog.Builder bd = new Builder(main.this);
bd.setMessage("mrGPS.apk\n版本:1.0\n作者:mrandexe");
bd.setTitle("关于");
bd.setPositiveButton("确认", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
}
});
bd.create().show();
break;
case MENU_EXIT:
exit();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
{
exit();
return true;
}
return super.onKeyDown(keyCode, event);
}

private void exit()
{
AlertDialog.Builder builder = new Builder(main.this);
builder.setMessage("确认退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
ActivityManager actMgr = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
actMgr.restartPackage(getPackageName());
}
});
builder.setNegativeButton("取消", new OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();

}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/tv" android:layout_height="wrap_content"></TextView>
</LinearLayout>





更多相关文章

  1. c语言规定在一个源程序中main函数的位置是哪里?
  2. 主函数在程序中的位置在哪
  3. C语言中main函数的位置可以是任意的么
  4. C语言中main函数可以在什么位置
  5. c语言规定在一个源程序中main函数的位置是什么?
  6. c源程序中main函数的位置是什么?
  7. 利用深度学习识别滑动验证码缺口位置
  8. Linux下查看PHP配置文件php.ini的位置
  9. php中如何在数组指定位置插入数据单元

随机推荐

  1. Android使用FFmpeg(二)--Android Studio
  2. Android五个布局
  3. Android SVG动画PathView源码解析与使用
  4. Android Framework 基础理解
  5. Google Developing for Android 三 - Perf
  6. Android- NDK编译APK中native死机调试
  7. 转载 android 利用ksoap2方式连接webserv
  8. 【Android 系统开发】 编译 Android文件
  9. Android 开发小技巧
  10. 通过ant脚本,编译打包android工程