2017-11-10 11 views
0

ユーザーの場所を常に保存してからサーバーに送信する必要があるアプリケーションを構築しています。そのために、私はFusedLocationApisをサービスで使用しています。初めて、ユーザーに場所をオンにするよう求めています。 これは完璧に動作しています。ユーザーが場所をオフにしているかどうかを確認する

ここで、ユーザーが誤って場所をオフにするとどうなりますか。彼にどのように知らせるべきですか?開発者はこれを世話しなければなりませんか、それとも単にユーザーに任せますか?

私は彼にこれについてのアンドロイド通知を与えることを考えました。そのためには、サービスで有効になっている場所を常に確認する必要があります。私はそのための作業コードを持っています(関数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"); 

} 

}

+0

オーバーライドを変更します。これは、シナリオのロジックを記述する場所です。詳細についてはhttps://developer.android.com/reference/android/location/LocationListener.htmlを参照してください。 – Jimmy

+0

可能な複製https://stackoverflow.com/questions/35496339/how-can-i-detect-if-user-disable-gps-android-play-services –

+0

@Jimmy私は以前にonProviderEnabled/Disabledメソッドをオーバーライドしようとしました。しかし、私は現在の実装ではそうすることができないと思います。 – Sanjeev

答えて

3

あなたは、ユーザーが手動で回転するときの位置状態を知るために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の状態を聞いて開始するActivityonCreate()に登録receiverActivity

public interface LocationCallBack { 
    /** 
    * on Location switch triggered 
    */ 
    void onLocationTriggered(); 
} 

にGPSの変更を伝えるために作成する方法がonProviderDisabled

public void onCreate(Bundle savedInstance){ 
//--- 
registerReceiver(new GpsReceiver(new LocationCallBack() { 
      @Override 
      public void onLocationTriggered() { 
       //Location state changed 
      } 
     }), new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)); 
//--- 
} 
0

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(); 
    } 
関連する問題