AndroidオリジナルGPSとネットワーク測位ツール類
5896 ワード
アプリケーション開発では、常にネットワークの位置を取得する方法が必要です.ここではツールクラスのパッケージを作成し、Androidのオリジナル位置決めを使用しています.2つの方法が含まれています.1つは、オリジナルGPSです.1つは、ネットワーク位置決めのパッケージ実装です.
:
package com.vondear.tools.tools;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.app.Activity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import com.ta.utdid2.android.utils.SharedPreferenceHelper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* Created by AndyYuan on time at 2019/6/20.
*/
public class GetGPSUtil {
static GetGPSUtil getGPSUtil;
public static GetGPSUtil getInstance() {
if (getGPSUtil == null) getGPSUtil = new GetGPSUtil();
return getGPSUtil;
}
/**
*
*
* @param context
* @return
*/
private String getLngAndLat(Context context) {
double latitude = 0.0;
double longitude = 0.0;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { // gps
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return " GPS ";
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {// GPS
Toast.makeText(context, "GPS ", Toast.LENGTH_SHORT).show();
return getLngAndLatWithNetwork(context);
}
} else { //
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
// Android GPS( GPS GPS) getLastKnownLocation(LoactionManager.NETWORK_PROVIDER) 。
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
return longitude + "," + latitude;
}
//
public String getLngAndLatWithNetwork(Context context) {
double latitude = 0.0;
double longitude = 0.0;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return " Internet ";
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
return longitude + "," + latitude;
}
LocationListener locationListener = new LocationListener() {
// Provider 、
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider enable , GPS
@Override
public void onProviderEnabled(String provider) {
}
// Provider disable , GPS
@Override
public void onProviderDisabled(String provider) {
}
// , Provider ,
@Override
public void onLocationChanged(Location location) {
}
};
}