真機不行,模擬器可以。手機有問題,先記錄代碼再說。
代碼如下:
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="basic.android.lesson26"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="7"/>
- <application android:label="@string/app_name">
- <activity android:name=".TestMyGPS"
- 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>
-
- <!-- 粗略定位授權 -->
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <!-- 精細定位授權 -->
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- </manifest>
-
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:id="@+id/button1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="ok"/>
- <TextView android:id="@+id/textView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Hello World, MainActivity"
- />
- <TextView android:id="@+id/show_status"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="init"/>
-
- <TextView android:id="@+id/temp_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="臨時"/>
- </LinearLayout>
TestMyGPS.java
- package basic.android.lesson26;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class TestMyGPS extends Activity {
-
- private static final String TAG = "TestMyGPS";
- Button mButton;
- TextView tv1;
- TextView mStatus;
- TextView mTemp;
- LocationManager mlm;
- LocationListener locationListener;
- String mFilter;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- // 定義UI組件
- mButton = (Button) findViewById(R.id.button1);
- tv1 = (TextView) findViewById(R.id.textView1);
- mStatus = (TextView) findViewById(R.id.show_status);
- mTemp = (TextView) findViewById(R.id.temp_text);
-
-
- mButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(android.view.View view) {
- // 轉至 GPS 設置界面
- Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
- startActivityForResult(intent, 0);
- }
- });
- // 獲取LocationManager對象
- mlm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
-
- // 定義Criteria對象
-
- // 獲取GPS信息提供者
- Criteria filter = getFilter();
- mFilter = mlm.getBestProvider(filter, true);
-
- // try {
- // mlm.setTestProviderEnabled(mFilter, true);
- // } catch (IllegalArgumentException e) {
- // String err = "IllegalArgumentException=" + e.getMessage();
- // Log.e(TAG, err);
- // Toast.makeText(this, err, Toast.LENGTH_LONG).show();
- // }
- // openGPS();
- gpsStatus();
- // 位置監聽器
- locationListener = new LocationListener() {
-
- // 當位置改變時觸發
- public void onLocationChanged(Location location) {
- updateLocation(location);
- Toast.makeText(TestMyGPS.this, "onLocationChanged=" + location, Toast.LENGTH_LONG).show();
- gpsStatus();
- mTemp.setText("onLocationChanged="+location);
- }
-
- // Provider失效時觸發
- public void onProviderDisabled(String arg0) {
- gpsStatus();
- mTemp.setText("onProviderDisabled=" + arg0);
- }
-
- // Provider可用時觸發
- public void onProviderEnabled(String arg0) {
- gpsStatus();
- mTemp.setText("onProviderEnabled=" + arg0);
- }
-
- // Provider狀態改變時觸發
- public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
- mTemp.setText("onStatusChanged=" + arg0);
- }
- };
-
- // 500毫秒更新一次,忽略位置變化
- mlm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, locationListener);
-
- }
-
-
- private void openGPS() {
- // if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
- // Toast.makeText(this, " 位置源已設置! ", Toast.LENGTH_SHORT).show();
- // return;
- // }
- //
- // Toast.makeText(this, " 位置源未設置! ", Toast.LENGTH_SHORT).show();
- }
-
- private Criteria getFilter() {
- Criteria criteria = new Criteria();
- // 設置定位精確度 Criteria.ACCURACY_COARSE 比較粗略, Criteria.ACCURACY_FINE則比較精細
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- // 設置是否需要海拔信息 Altitude
- criteria.setAltitudeRequired(false);
- // 設置是否需要方位信息 Bearing
- criteria.setBearingRequired(false);
- // 設置是否允許運營商收費
- criteria.setCostAllowed(true);
- // 設置對電源的需求
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- return criteria;
- }
-
- private void gpsStatus() {
- if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
- mStatus.setText("GPS開啟");
- } else {
- mStatus.setText("GPS未開啟");
- Toast.makeText(this, " 位置源未設置! ", Toast.LENGTH_SHORT).show();
- // 轉至 GPS 設置界面
- Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
- startActivityForResult(intent, 0);
- }
- }
-
- // 更新位置信息
- private void updateLocation(Location location) {
- if (location != null) {
- tv1.setText("更新位置:" + location.toString() + "\n\t其中經度:" + location.getLongitude() + "\n\t其中緯度:"
- + location.getLatitude());
- } else {
- tv1.setText("更新位置失敗");
- }
- }
-
- @Override
- protected void onDestroy() {
- mlm.removeUpdates(locationListener);
- // mlm.setTestProviderEnabled(mFilter, false);
- super.onDestroy();
- }
- }