編輯:關於Android編程
android自帶gps定位功能相信大家都不會太陌生了,都有所涉及。簡單的寫了一個示例程序,獲取經緯度還有其它相關數據的代碼,還有其他相關的知識,比如直接跳轉到打開系統gps設置的界面。還有一個bug的處理,異常信息:Incomplete location object, missing timestamp or accuracy
package com.example.locationexample;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
LocationManager mLocationManager;
Location mlocation;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textView1);
getLocation();
// mlocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
// mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mlocation);
}
public Location getLocation(){
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mlocation == null) {
mlocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, mLocationListener);
return mlocation;
}
LocationListener mLocationListener = new LocationListener() {
@TargetApi(17)
@Override
public void onLocationChanged(Location mlocal) {
if(mlocal == null) return;
String strResult = "getAccuracy:" + mlocal.getAccuracy() + "\r\n"
+ "getAltitude:" + mlocal.getAltitude() + "\r\n"
+ "getBearing:" + mlocal.getBearing() + "\r\n"
+ "getElapsedRealtimeNanos:" + String.valueOf(mlocal.getElapsedRealtimeNanos()) + "\r\n"
+ "getLatitude:" + mlocal.getLatitude() + "\r\n"
+ "getLongitude:" + mlocal.getLongitude() + "\r\n"
+ "getProvider:" + mlocal.getProvider()+ "\r\n"
+ "getSpeed:" + mlocal.getSpeed() + "\r\n"
+ "getTime:" + mlocal.getTime() + "\r\n";
Log.i("Show", strResult);
if (mTextView != null) {
mTextView.setText(strResult);
}
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String provider, int event, Bundle extras) {
}
};
}
權限如下:
LocationManager.GPS_PROVIDER是gps提供,100是時間,多久刷新一次,0是距離,如果你不動的話,測試最好寫0
mLocationListener是接口啦,數據就是那裡來的。
如果你使用mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mlocation);這行代碼報出下面的堆棧錯誤信息
java.lang.IllegalArgumentException: Incomplete location object, missing timestamp or accuracy? Location[gps 23.126704,113.365648 acc=1 et=?!? alt=-7.422 vel=0.008333334 bear=237.76]
方法一:
主要是這句,location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());,其實加上這句就可以了。
例如:
@SuppressLint("NewApi")
private Location getLoc(String provider)
{
Location location = new Location(provider);
location.setLatitude(lat);
location.setLongitude(lng);
location.setAltitude(altitude);
location.setBearing(bearing);
location.setSpeed(speed);
location.setAccuracy(accuracy);
location.setTime(System.currentTimeMillis());
location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
return location;
}
主要是這句,location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());,其實加上這句就可以了。如下
mlocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mlocation);
try
{
Method method = Location.class.getMethod("makeComplete");
if (method != null)
{
method.invoke(localLocation);
}
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
源碼如下:
/**
* Helper to fill incomplete fields.
*
*
Used to assist in backwards compatibility with
* Location objects received from applications.
*
* @see #isComplete
* @hide
*/
public void makeComplete() {
if (mProvider == null) mProvider = "?";
if (!mHasAccuracy) {
mHasAccuracy = true;
mAccuracy = 100.0f;
}
if (mTime == 0) mTime = System.currentTimeMillis();
if (mElapsedRealtimeNanos == 0) mElapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos();
}
建議使用第一個方法。
//獲取是否已打開自身GPS
public boolean isGpsEnable() {
String providers = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (providers.contains(LocationManager.GPS_PROVIDER)) {
return true;
} else {
return false;
}
}
Intent callGPSSettingIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); this.startActivity(callGPSSettingIntent);
Android statusbar 透明後 toolbar與之重疊問題的解決方法
之前想要給statusbar和toolbar實現這樣的效果:為使得statusbar變為透明,在自定義theme中給statusbar添加了以下屬性: &l
Android四大組件之ContentProvider(下)
(一)概述學完上一節,相信你已經知道如何去使用系統提供的ContentProvider或者自定義ContentProvider了, 已經基本滿足日常開發的需求了,有趣的是
Android 開發藝術探索——第十章 Android的消息機制
概述Android的消息機制主要值得就是Handler的運行機制,Handler的運行需要底層的MessageQueue和Looper的支撐。MessageQueue即為
Android應用Preference相關及源碼淺析(Preference組件家族篇)
1 前言前一篇(點我閱讀前一篇《Android應用Preference相關及源碼淺析(SharePreferences篇)》)我們討論分析使用了Android的Share
Android開發本地及網絡Mp3音樂播放器(十二)創建NetMusicListAdapter、SearchResult顯示網絡音樂列表
實現功能:實現NetMusicListAdapter(網絡音樂列表適配器