...
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
...
}
...
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="plop.myapplication"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.AppCompatTextView android:id="@+id/tvGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@null" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <android.support.v7.widget.AppCompatButton android:id="@+id/btGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click pour activer le GPS" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
package plop.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.location.LocationManager; import android.os.Build; import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatTextView; import android.view.View; public class MainActivity extends AppCompatActivity { private AppCompatButton mBtGPS; private AppCompatTextView mTvGPS; private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction() != null && intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) { /* normalement tu ne passeras jamais ici etant donné que le test est fait dans le onResume et que pour modifier l'état du GPS il faut passer par activity_onPause et activity_onResume */ updateUI("Le GPS est activé depuis le receiver"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtGPS = findViewById(R.id.btGPS); mTvGPS = findViewById(R.id.tvGPS); /* On effectue l'activation sur click du bouton */ mBtGPS.setOnClickListener((v) -> startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) ); } @Override public void onResume() { super.onResume(); /* Enregistrement du receiver pour connaitre l'etat du GPS au runtime */ registerReceiver(mReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)); updateUI("Le GPS est activé depuis le resume"); } public void onPause() { super.onPause(); unregisterReceiver(mReceiver); } private void updateUI(String text) { if(!isLocationEnabled(this)) { mBtGPS.setVisibility(View.VISIBLE); mTvGPS.setVisibility(View.GONE); } else { mBtGPS.setVisibility(View.GONE); mTvGPS.setVisibility(View.VISIBLE); mTvGPS.setText(text); } } /* Test de la disponibilité du GPS */ public static Boolean isLocationEnabled(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return lm.isLocationEnabled(); } else { return lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } } }