前言:
现时咱们对“gps filter”都比较关怀,你们都想要剖析一些“gps filter”的相关内容。那么小编同时在网上汇集了一些关于“gps filter””的相关文章,希望你们能喜欢,朋友们一起来学习一下吧!今天学习一下在手机中获取GPS经纬度,如果你也是初学者,可以参考一下本文,或许对你的学习有帮助。直接上代码
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=";
package="com.aj.GPS" >
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<!--允许使用PowerManager的 WakeLocks保持进程在休眠时从屏幕消失 -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<!-- 允许一个程序接收到ACTION_BOOT_COMPLETED广播在系统完成启动 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- 改变WiFi多播状态 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<!-- 允许程序读取底层系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- since 1.6.0 -->
<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_OWNER_DATA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- 关闭应用程序的权限 -->
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
实现工具类
package com.tool;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
public class LocationUtils {
private volatile static LocationUtils uniqueInstance;
private LocationManager locationManager;
private String locationProvider;
private Location location;
private Context mContext;
private String TAG="嗯";
private LocationUtils(Context context) {
mContext = context;
getLocation();
}
//采用Double CheckLock(DCL)实现单例
public static LocationUtils getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (LocationUtils.class) {
if (uniqueInstance == null) {
uniqueInstance = new LocationUtils( context );
}
}
}
return uniqueInstance;
}
private void getLocation() {
//1.获取位置管理器
locationManager = (LocationManager) mContext.getSystemService( Context.LOCATION_SERVICE );
//2.获取位置提供器,GPS或是NetWork
List<String> providers = locationManager.getProviders( true );
if (providers.contains( LocationManager.NETWORK_PROVIDER )) {
//如果是网络定位
Log.d( TAG, "如果是网络定位" );
locationProvider = LocationManager.NETWORK_PROVIDER;
} else if (providers.contains( LocationManager.GPS_PROVIDER )) {
//如果是GPS定位
Log.d( TAG, "如果是GPS定位" );
locationProvider = LocationManager.GPS_PROVIDER;
} else {
Log.d( TAG, "没有可用的位置提供器" );
return;
}
// 需要检查权限,否则编译报错,想抽取成方法都不行,还是会报错。只能这样重复 code 了。
// if (Build.VERSION.SDK_INT >= 23 &&
// ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
// ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
// return;
// }
// if (ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
// return;
// }
//3.获取上次的位置,一般第一次运行,此值为null
Location location = locationManager.getLastKnownLocation( locationProvider );
if (location != null) {
setLocation( location );
}
// 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager.requestLocationUpdates( locationProvider, 0, 0, locationListener );
}
private void setLocation(Location location) {
this.location = location;
String address = "纬度:" + location.getLatitude() + "经度:" + location.getLongitude();
Log.d( TAG, address );
}
//获取经纬度
public Location showLocation() {
return location;
}
// 移除定位监听
public void removeLocationUpdatesListener() {
// 需要检查权限,否则编译不过
// if (Build.VERSION.SDK_INT >= 23 &&
// ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
// ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
// return;
// }
if (locationManager != null) {
uniqueInstance = null;
locationManager.removeUpdates( locationListener );
}
}
/**
* LocationListern监听器
* 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
*/
LocationListener locationListener = new LocationListener() {
/**
* 当某个位置提供者的状态发生改变时
*/
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
/**
* 某个设备打开时
*/
@Override
public void onProviderEnabled(String provider) {
}
/**
* 某个设备关闭时
*/
@Override
public void onProviderDisabled(String provider) {
}
/**
* 手机位置发生变动
*/
@Override
public void onLocationChanged(Location location) {
location.getAccuracy();//精确度
setLocation( location );
}
};
}
调用
package com.aj.GPS;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.aj.GPS.MainActivity;
import com.tool.LocationUtils;
public class MainActivity extends Activity {
private Context ct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ct=this;
Button btn = (Button) findViewById( R.id.activitymainButton1);
final TextView text = (TextView) findViewById( R.id.activitymainTextView1);
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Location location = LocationUtils.getInstance( MainActivity.this ).showLocation();
if (location != null) {
String address = "纬度:" + location.getLatitude() + "经度:" + location.getLongitude();
Log.d( "FLY.LocationUtils", address );
text.setText( address );
}
}
} );
}
}
标签: #gps filter