真机不行,模拟器可以。手机有问题,先记录代码再说。
代码如下:
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();
}
}