2012-03-29 12 views
10

मैं एक एंड्रॉइड ऐप विकसित कर रहा हूं जिसे जीपीएस को सक्रिय करने की जरूरत है।एंड्रॉइड पर जीपीएस चालू करने के लिए कैसे करें

मैं मंचों का एक बहुत में विषयों की एक बहुत कुछ पढ़ा है और इस सवाल का जवाब मैंने पाया है:

यह संभव नहीं है

लेकिन ... "Cerberus" एपीपी बदल जाता है मेरी जीपीएस ... तो ... यह संभव है!

क्या कोई इस से मेरी सहायता कर सकता है?

+0

यहाँ code- है [मुड़ें ओ n या जीपीएस बंद प्रोग्राम के रूप में] [1] [1]:। http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker केवल एंड्रॉयड पर – Rajkiran

+0

काम <2.3.3 –

उत्तर

14

कोई असंभव नहीं है, और अनुचित है। आप केवल अपने अधिकार के बिना उपयोगकर्ता फोन का प्रबंधन नहीं कर सकते हैं।

प्ले स्टोर से:

"Cerberus स्वचालित रूप से जीपीएस सक्षम बनाता है अगर यह बंद है जब आप (केवल Android < 2.3.3 पर) अपने डिवाइस स्थानीय बनाना कोशिश करते हैं और आप अनधिकृत: अनइंस्टॉल से बचाने कर सकते हैं - अधिक जानकारी के ऐप कॉन्फ़िगरेशन में। "

आप कुछ इस तरह कर सकते हैं:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
+1

ठीक है ... मैं जो डिवाइस उपयोग करता हूं वह 2.3.3 + है ... इसलिए ... उत्तर के लिए धन्यवाद ... –

+1

@goodm: FYI, यह असंभव नहीं है। लेकिन हाँ, यह अनुचित है। – Rajkiran

+3

ओला कैब कैसे कर रहे हैं? एक ही प्रॉम्प्ट में वे जीपीएस सेटिंग पैनल के लिए उपयोगकर्ता को बिना छेड़छाड़ किए जीपीएस सक्षम कर रहे हैं। – Alpesh

11

वहां एक शोषण होता था जिसने किसी विशेष अनुमति के साथ किसी ऐप द्वारा जीपीएस को चालू करने की अनुमति दी थी। यह शोषण अब 2.3 (अधिकांश रोम में) के रूप में मौजूद नहीं है। यहाँ एक और पद है कि इसके बारे में बात करती है,

How can I enable or disable the GPS programmatically on Android?

"जीपीएस सक्षम", एक सुरक्षित सेटिंग है तो आप WRITE_SECURE_SETTINGS अनुमति होना आवश्यक है। यह एक हस्ताक्षर संरक्षित अनुमति है हालांकि, इसलिए ऐप को यह तब तक नहीं दिया जाएगा जब तक कि यह निर्माता के प्लेटफ़ॉर्म प्रमाणपत्र से हस्ताक्षरित न हो।

सही बात यह है कि उपयोगकर्ता को स्थान सेटिंग पृष्ठ पर भेजना है, और यदि वे चाहें तो उन्हें जीपीएस सक्षम करने दें। उदाहरण के लिए,

Intent i = new 
Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(i); 
1

आप इस थ्रेड

How can I enable or disable the GPS programmatically on Android?

यहाँ कोड है कि धागा

private void turnGPSOn(){ 
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 

    if(!provider.contains("gps")){ //if gps is disabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     sendBroadcast(poke); 
    } 
} 

private void turnGPSOff(){ 
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 

    if(provider.contains("gps")){ //if gps is enabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     sendBroadcast(poke); 


    } 
} 

से नकल कर रहे हैं की जाँच करने के लिए चाहते हो सकता है लेकिन समाधान की सिफारिश नहीं की जाती है क्योंकि यह उपलब्ध नहीं होगा le Android संस्करण> 2.3 supposingly .. टिप्पणी

5
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(R.string.gps_disabled_message) 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent);     
      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

यह एक चेतावनी बनाता है और उपयोगकर्ता सेटिंग स्क्रीन पर जाएं और सही अपने अनुप्रयोग के लिए वापस आने के लिए वापस बटन हिट करने के लिए अनुमति देता है की जाँच करें। पावर विजेट शोषण मेरे ज्ञान के लिए 2.3 से अधिक काम नहीं करता है।

2

इस कोड का प्रयोग करें // turnGPSON कहा जाता है के बाद setcontentView (xml)

private void turnGPSOn() { 

    String provider = android.provider.Settings.Secure.getString(
      getContentResolver(), 
      android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if (!provider.contains("gps")) { // if gps is disabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", 
       "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     sendBroadcast(poke); 
    } 
}** 
0

मुझे लगता है कि हम सिर्फ गूगल मानचित्र काम करता है की तरह सेटिंग्स को खोले बिना स्थान सक्षम करने के लिए और अधिक बेहतर संस्करण है।

यह होगा इस तरह दिखता है -

enter image description here

Gradle में निर्भरता जोड़ें - संकलन 'com.google.android.gms: खेलने-सेवाओं-स्थान: 10.0.1'

public class MapActivity extends AppCompatActivity { 

    protected static final String TAG = "LocationOnOff"; 


    private GoogleApiClient googleApiClient; 
    final static int REQUEST_LOCATION = 199; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.setFinishOnTouchOutside(true); 

     // Todo Location Already on ... start 
     final LocationManager manager = (LocationManager) MapActivity.this.getSystemService(Context.LOCATION_SERVICE); 
     if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(MapActivity.this)) { 
      Toast.makeText(MapActivity.this,"Gps already enabled",Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
     // Todo Location Already on ... end 

     if(!hasGPSDevice(MapActivity.this)){ 
      Toast.makeText(MapActivity.this,"Gps not Supported",Toast.LENGTH_SHORT).show(); 
     } 

     if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(MapActivity.this)) { 
      Log.e("TAG","Gps already enabled"); 
      Toast.makeText(MapActivity.this,"Gps not enabled",Toast.LENGTH_SHORT).show(); 
      enableLoc(); 
     }else{ 
      Log.e("TAG","Gps already enabled"); 
      Toast.makeText(MapActivity.this,"Gps already enabled",Toast.LENGTH_SHORT).show(); 
     } 
    } 


    private boolean hasGPSDevice(Context context) { 
     final LocationManager mgr = (LocationManager) context 
       .getSystemService(Context.LOCATION_SERVICE); 
     if (mgr == null) 
      return false; 
     final List<String> providers = mgr.getAllProviders(); 
     if (providers == null) 
      return false; 
     return providers.contains(LocationManager.GPS_PROVIDER); 
    } 

    private void enableLoc() { 

     if (googleApiClient == null) { 
      googleApiClient = new GoogleApiClient.Builder(MapActivity.this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
         @Override 
         public void onConnected(Bundle bundle) { 

         } 

         @Override 
         public void onConnectionSuspended(int i) { 
          googleApiClient.connect(); 
         } 
        }) 
        .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
         @Override 
         public void onConnectionFailed(ConnectionResult connectionResult) { 

          Log.d("Location error","Location error " + connectionResult.getErrorCode()); 
         } 
        }).build(); 
      googleApiClient.connect(); 

      LocationRequest locationRequest = LocationRequest.create(); 
      locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
      locationRequest.setInterval(30 * 1000); 
      locationRequest.setFastestInterval(5 * 1000); 
      LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
        .addLocationRequest(locationRequest); 

      builder.setAlwaysShow(true); 

      PendingResult<LocationSettingsResult> result = 
        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
      result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
       @Override 
       public void onResult(LocationSettingsResult result) { 
        final Status status = result.getStatus(); 
        switch (status.getStatusCode()) { 
         case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
          try { 
           // Show the dialog by calling startResolutionForResult(), 
           // and check the result in onActivityResult(). 
           status.startResolutionForResult(MapActivity.this, REQUEST_LOCATION); 

           finish(); 
          } catch (IntentSender.SendIntentException e) { 
           // Ignore the error. 
          } 
          break; 
        } 
       } 
      }); 
     } 
    } 

} 
0
public class NearestActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 
    private Location mylocation; 
    private GoogleApiClient googleApiClient; 
    private final static int REQUEST_CHECK_SETTINGS_GPS=0x1; 
    private final static int REQUEST_ID_MULTIPLE_PERMISSIONS=0x2; 
    public static double gpsLat, gpsLong; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this, 0, this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mylocation = location; 
     if (mylocation != null) { 
      gpsLat = mylocation.getLatitude(); 
      gpsLong = mylocation.getLongitude(); 
      if(adapter.getItemCount()!=0) { 
       Collections.sort(list, new Comparator<TokoUpload>() { 
        @Override 
        public int compare(TokoUpload o1, TokoUpload o2) { 
         return o1.getMeter() - o2.getMeter(); 
        } 
       }); 
       adapter.notifyDataSetChanged(); 
      } 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     checkPermissions(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    private void getMyLocation(){ 
     if(googleApiClient != null) { 
      if (googleApiClient.isConnected()) { 
       int permissionLocation = ContextCompat.checkSelfPermission(NearestTokoActivity.this, 
         android.Manifest.permission.ACCESS_FINE_LOCATION); 
       if (permissionLocation == PackageManager.PERMISSION_GRANTED) { 
        mylocation =      LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
        LocationRequest locationRequest = new LocationRequest(); 
        locationRequest.setInterval(3000); 
        locationRequest.setFastestInterval(3000); 
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
          .addLocationRequest(locationRequest); 
        builder.setAlwaysShow(true); 
        LocationServices.FusedLocationApi 
          .requestLocationUpdates(googleApiClient, locationRequest, this); 
        PendingResult<LocationSettingsResult> result = 
          LocationServices.SettingsApi 
            .checkLocationSettings(googleApiClient, builder.build()); 
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 

         @Override 
         public void onResult(LocationSettingsResult result) { 
          final Status status = result.getStatus(); 
          switch (status.getStatusCode()) { 
           case LocationSettingsStatusCodes.SUCCESS: 
            int permissionLocation = ContextCompat 
              .checkSelfPermission(NearestTokoActivity.this, 
                android.Manifest.permission.ACCESS_FINE_LOCATION); 
            if (permissionLocation == PackageManager.PERMISSION_GRANTED) { 
             mylocation = LocationServices.FusedLocationApi 
               .getLastLocation(googleApiClient); 
            } 
            break; 
           case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
            try { 
             status.startResolutionForResult(NearestTokoActivity.this, 
               REQUEST_CHECK_SETTINGS_GPS); 
            } catch (IntentSender.SendIntentException ignored) { 
            } 
            break; 
           case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
            break; 
          } 
         } 
        }); 
       } 
      } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
      case REQUEST_CHECK_SETTINGS_GPS: 
       switch (resultCode) { 
        case Activity.RESULT_OK: 
         getMyLocation(); 
         break; 
        case Activity.RESULT_CANCELED: 
         finish(); 
         break; 
       } 
       break; 
     } 
    } 

    private void checkPermissions(){ 
     int permissionLocation = ContextCompat.checkSelfPermission(NearestTokoActivity.this, 
       android.Manifest.permission.ACCESS_FINE_LOCATION); 
     List<String> listPermissionsNeeded = new ArrayList<>(); 
     if (permissionLocation != PackageManager.PERMISSION_GRANTED) { 
      listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION); 
      if (!listPermissionsNeeded.isEmpty()) { 
       ActivityCompat.requestPermissions(this, 
         listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS); 
      } 
     }else{ 
      getMyLocation(); 
     } 

    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     int permissionLocation = ContextCompat.checkSelfPermission(NearestTokoActivity.this, 
       android.Manifest.permission.ACCESS_FINE_LOCATION); 
     if (permissionLocation == PackageManager.PERMISSION_GRANTED) { 
      getMyLocation(); 
     } 
    } 
} 
संबंधित मुद्दे