簡體   English   中英

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");

}

}

您可以使用位置狀態更改的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));
//---
}

您可以使用以下方式查看位置可用性:

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();
    }

此代碼完全適用於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");
            }
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM