Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void startGpsService() {
- handler = new Handler();
- task = new Runnable() {
- @Override
- public void run() {
- gps = new GPSTracker(getApplicationContext());
- if (gps.canGetLocation()) {
- ... do something
- } else {
- // GPS or Network is not enabled
- gps.showSettingsAlert();
- }
- handler.postDelayed(task, 10000);
- }
- };
- task.run();
- }
- public class GPSTracker extends Service implements LocationListener {
- private final Context mContext;
- // Declaring a Location Manager
- protected LocationManager locationManager;
- public GPSTracker(Context context) {
- this.mContext = context;
- getLocation();
- }
- ...
- /**
- * Function to show settings alert dialog
- * On pressing Settings button will lauch Settings Options
- * */
- public void showSettingsAlert(){
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
- // Setting Dialog Title
- alertDialog.setTitle("GPS is settings");
- // Setting Dialog Message
- alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
- // On pressing Settings button
- alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,int which) {
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- mContext.startActivity(intent);
- }
- });
- // on pressing cancel button
- alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- // Showing Alert Message
- alertDialog.show();
- }
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement