Advertisement
jajangjaenalyusup

Cek GPS

Dec 8th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1.  private void cekGPS() {
  2.     try {
  3.         // pengecekan GPS hidup / tidak
  4.         LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  5.         if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  6.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  7.             builder.setTitle("Info");
  8.             builder.setMessage("Anda akan mengaktifkan GPS?");
  9.             builder.setPositiveButton("Ya",new DialogInterface.OnClickListener() {
  10.  
  11.                 @Override
  12.                 public void onClick(DialogInterface dialog, int which) {
  13.                     // TODO Auto-generated method stub
  14.                     Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  15.                     startActivity(intent);
  16.                 }
  17.             });
  18.             builder.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
  19.  
  20.                 @Override
  21.                 public void onClick(DialogInterface dialog, int which) {
  22.                     // TODO Auto-generated method stub
  23.                     dialog.dismiss();
  24.                 }
  25.             });
  26.             builder.create().show();
  27.         }
  28.     } catch (Exception e) {
  29.  
  30.     }
  31.     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
  32.  
  33.     // menampilkan status google play service
  34.     if (status != ConnectionResult.SUCCESS) {
  35.         int requestCode = 10;
  36.         Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,requestCode);
  37.             dialog.show();
  38.     } else {
  39.     // google play service tersedia
  40.         try {
  41.             LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  42.  
  43.             // membuat kriteria untuk menampung provider
  44.             Criteria criteria = new Criteria();
  45.  
  46.             // mencari provider terbaik
  47.             String provider = locationManager.getBestProvider(criteria, true);
  48.  
  49.             // mendapatkan lokasi terakhir
  50.             Location location = locationManager.getLastKnownLocation(provider);
  51.  
  52.             if (location != null) {
  53.                 onLocationChanged(location);
  54.             }
  55.  
  56.             locationManager.requestLocationUpdates(provider, 5000, 0, this);
  57.         } catch (Exception e) {
  58.  
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement