android 检查用户是否关闭了定位
android check if user turned off location
我正在构建一个应用程序,我必须在其中不断保存用户的位置,然后将其发送到服务器。为此,我在服务中使用 FusedLocationApis。我第一次要求用户打开位置。
它运行良好。
现在,如果用户无意中关闭了位置信息怎么办。我该怎么通知他?开发人员是否必须处理这个问题,还是将其留给用户?
我想为此向他发出 android 通知。为此,我需要不断检查我们在服务中启用的位置。我有一个工作代码(函数 checkHighAccuracyLocationMode)。但是我应该在哪里进行检查(checkHighAccuracyLocationMode)?
每次在此服务中启动哪个功能?或者,如果无论如何我可以获得一个在用户关闭其位置时启动的功能?
这是我的服务:
public class locationUpdatesService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Location mLastLocation;
private Location mCurrentLocation;
private String mLastUpdateTime;
private dataBaseHandler db_helper;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
initialise();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
db_helper = new dataBaseHandler(getApplicationContext(),dataBaseHandler.DATABASE_NAME,null,1);
}
@Override
public void onDestroy() {
stopLocationUpdates();
singletGoogleClientApi.setinstancenull();
singletLocationRequest.setinstancenull();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void initialise(){
Log.e("ghgdhu","qaws");
if (singletGoogleClientApi.getinstance().getGoogleApiCLient() == null) {
singletGoogleClientApi.getinstance().setmSingletGoogleApiClient(new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build());
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
if(!singletGoogleClientApi.getinstance().getGoogleApiCLient().isConnected()){
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
}
protected void createLocationRequest() {
if(singletLocationRequest.getinstance().getLocationRequest() == null) {
singletLocationRequest.getinstance().setSingletLocationRequest(new LocationRequest()
.setInterval(10000)
.setFastestInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
}
}
public static boolean checkHighAccuracyLocationMode(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//Equal or higher than API 19/KitKat
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY){
return true;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}else{
//Lower than API 19
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){
return true;
}
}
return false;
}
@Override
public void onLocationChanged(Location location) {
Log.e("qazxsw","inONLocationCHanged");
mCurrentLocation = location;
Log.e("loc : ",Double.toString(mCurrentLocation.getLatitude()));
Toast.makeText(this, "My Location: "+Double.toString(mCurrentLocation.getLatitude())+ " , " + Double.toString(mCurrentLocation.getLongitude()),
Toast.LENGTH_SHORT).show();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
if(!checkHighAccuracyLocationMode(getBaseContext())){
Log.e("turned OFF","LOCATION");
}
else {
Log.e("ONNNNN","LOCATION");
}
db_helper.Insert(mLastUpdateTime,mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(singletGoogleClientApi.
getinstance().getGoogleApiCLient(), this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
protected void startLocationUpdates() {
try {
if(singletLocationRequest.getinstance().getLocationRequest()!=null &&
singletGoogleClientApi.getinstance().getGoogleApiCLient()!=null){
Log.e("requesting","yES BRO");
LocationServices.FusedLocationApi.requestLocationUpdates(
singletGoogleClientApi.getinstance().getGoogleApiCLient(), singletLocationRequest.getinstance().getLocationRequest(), this);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
singletGoogleClientApi.getinstance().getGoogleApiCLient());
}
else {
initialise();
}
}
catch (SecurityException e) {
e.getStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e("ON CONNECTION SUSPENDED","PETROL");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("COnnection ","DIESEL");
}
}
您可以使用以下方式查看位置可用性:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch (Exception ex){}
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch (Exception ex){}
if(!gps_enabled && !network_enabled){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Startup.this.startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
您可以使用位置状态更改 receiver
来了解用户何时手动关闭位置
GpsReceiver.java
public class GpsReceiver extends BroadcastReceiver {
private final LocationCallBack locationCallBack;
/**
* initializes receiver with callback
* @param iLocationCallBack Location callback
*/
public GpsReceiver(LocationCallBack iLocationCallBack){
this.locationCallBack = iLocationCallBack;
}
/**
* triggers on receiving external broadcast
* @param context Context
* @param intent Intent
*/
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
locationCallBack.onLocationTriggered();
}
}
}
创建一个 interface
以将 GPS 更改传达给 Activity
public interface LocationCallBack {
/**
* on Location switch triggered
*/
void onLocationTriggered();
}
在 Activity
的 onCreate()
中注册 receiver
以开始监听 GPS 状态变化
public void onCreate(Bundle savedInstance){
//---
registerReceiver(new GpsReceiver(new LocationCallBack() {
@Override
public void onLocationTriggered() {
//Location state changed
}
}), new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
//---
}
此代码完美适用于 Android 9 (Pie) 版本
主要活动
public class MainActivity extends AppCompatActivity {
private MyLocationReceiver mLocationReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationReceiver = new MyLocationReceiver(this, Snackbar.make(findViewById(android.R.id.content), "Location service is not enabled", Snackbar.LENGTH_INDEFINITE));
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mLocationReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(mLocationReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MyLocationReceiver
public class MyLocationReceiver extends BroadcastReceiver {
private static final String TAG = "MyLocationReceiver";
private Context context;
private Snackbar snackbar;
public MyLocationReceiver(Context context, Snackbar snackbar){
this.context = context;
this.snackbar = snackbar;
}
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsEnabled && networkEnabled) {
if (snackbar != null) {
snackbar.dismiss();
}
Log.d(TAG, "GPS is enabled");
} else {
snackbar.show();
Log.d(TAG, "GPS is disabled");
}
}
}
}
我正在构建一个应用程序,我必须在其中不断保存用户的位置,然后将其发送到服务器。为此,我在服务中使用 FusedLocationApis。我第一次要求用户打开位置。 它运行良好。
现在,如果用户无意中关闭了位置信息怎么办。我该怎么通知他?开发人员是否必须处理这个问题,还是将其留给用户?
我想为此向他发出 android 通知。为此,我需要不断检查我们在服务中启用的位置。我有一个工作代码(函数 checkHighAccuracyLocationMode)。但是我应该在哪里进行检查(checkHighAccuracyLocationMode)? 每次在此服务中启动哪个功能?或者,如果无论如何我可以获得一个在用户关闭其位置时启动的功能?
这是我的服务:
public class locationUpdatesService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Location mLastLocation;
private Location mCurrentLocation;
private String mLastUpdateTime;
private dataBaseHandler db_helper;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
initialise();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
db_helper = new dataBaseHandler(getApplicationContext(),dataBaseHandler.DATABASE_NAME,null,1);
}
@Override
public void onDestroy() {
stopLocationUpdates();
singletGoogleClientApi.setinstancenull();
singletLocationRequest.setinstancenull();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void initialise(){
Log.e("ghgdhu","qaws");
if (singletGoogleClientApi.getinstance().getGoogleApiCLient() == null) {
singletGoogleClientApi.getinstance().setmSingletGoogleApiClient(new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build());
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
if(!singletGoogleClientApi.getinstance().getGoogleApiCLient().isConnected()){
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
}
protected void createLocationRequest() {
if(singletLocationRequest.getinstance().getLocationRequest() == null) {
singletLocationRequest.getinstance().setSingletLocationRequest(new LocationRequest()
.setInterval(10000)
.setFastestInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
}
}
public static boolean checkHighAccuracyLocationMode(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//Equal or higher than API 19/KitKat
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY){
return true;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}else{
//Lower than API 19
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){
return true;
}
}
return false;
}
@Override
public void onLocationChanged(Location location) {
Log.e("qazxsw","inONLocationCHanged");
mCurrentLocation = location;
Log.e("loc : ",Double.toString(mCurrentLocation.getLatitude()));
Toast.makeText(this, "My Location: "+Double.toString(mCurrentLocation.getLatitude())+ " , " + Double.toString(mCurrentLocation.getLongitude()),
Toast.LENGTH_SHORT).show();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
if(!checkHighAccuracyLocationMode(getBaseContext())){
Log.e("turned OFF","LOCATION");
}
else {
Log.e("ONNNNN","LOCATION");
}
db_helper.Insert(mLastUpdateTime,mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(singletGoogleClientApi.
getinstance().getGoogleApiCLient(), this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
protected void startLocationUpdates() {
try {
if(singletLocationRequest.getinstance().getLocationRequest()!=null &&
singletGoogleClientApi.getinstance().getGoogleApiCLient()!=null){
Log.e("requesting","yES BRO");
LocationServices.FusedLocationApi.requestLocationUpdates(
singletGoogleClientApi.getinstance().getGoogleApiCLient(), singletLocationRequest.getinstance().getLocationRequest(), this);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
singletGoogleClientApi.getinstance().getGoogleApiCLient());
}
else {
initialise();
}
}
catch (SecurityException e) {
e.getStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e("ON CONNECTION SUSPENDED","PETROL");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("COnnection ","DIESEL");
}
}
您可以使用以下方式查看位置可用性:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch (Exception ex){}
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch (Exception ex){}
if(!gps_enabled && !network_enabled){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Startup.this.startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
您可以使用位置状态更改 receiver
来了解用户何时手动关闭位置
GpsReceiver.java
public class GpsReceiver extends BroadcastReceiver {
private final LocationCallBack locationCallBack;
/**
* initializes receiver with callback
* @param iLocationCallBack Location callback
*/
public GpsReceiver(LocationCallBack iLocationCallBack){
this.locationCallBack = iLocationCallBack;
}
/**
* triggers on receiving external broadcast
* @param context Context
* @param intent Intent
*/
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
locationCallBack.onLocationTriggered();
}
}
}
创建一个 interface
以将 GPS 更改传达给 Activity
public interface LocationCallBack {
/**
* on Location switch triggered
*/
void onLocationTriggered();
}
在 Activity
的 onCreate()
中注册 receiver
以开始监听 GPS 状态变化
public void onCreate(Bundle savedInstance){
//---
registerReceiver(new GpsReceiver(new LocationCallBack() {
@Override
public void onLocationTriggered() {
//Location state changed
}
}), new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
//---
}
此代码完美适用于 Android 9 (Pie) 版本
主要活动
public class MainActivity extends AppCompatActivity {
private MyLocationReceiver mLocationReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationReceiver = new MyLocationReceiver(this, Snackbar.make(findViewById(android.R.id.content), "Location service is not enabled", Snackbar.LENGTH_INDEFINITE));
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mLocationReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(mLocationReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MyLocationReceiver
public class MyLocationReceiver extends BroadcastReceiver {
private static final String TAG = "MyLocationReceiver";
private Context context;
private Snackbar snackbar;
public MyLocationReceiver(Context context, Snackbar snackbar){
this.context = context;
this.snackbar = snackbar;
}
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsEnabled && networkEnabled) {
if (snackbar != null) {
snackbar.dismiss();
}
Log.d(TAG, "GPS is enabled");
} else {
snackbar.show();
Log.d(TAG, "GPS is disabled");
}
}
}
}