android gps開發

來源:互聯網
上載者:User

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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.