2016-10-19 4 views
18

मैनिफेस्ट फ़ाइल में मैंने अनुमतियों को मोटे और ठीक जोड़ा, और जब मैं एंड्रॉइड 6 के साथ डिवाइस पर चलाता हूं, कुछ भी नहीं होता! मैं सबकुछ कोशिश करता हूं लेकिन स्थान अपडेट प्राप्त करने का कोई तरीका नहीं ...एंड्रॉइड पर रनटाइम पर स्थान अनुमति का अनुरोध कैसे करें 6

मैं क्या गलत कर रहा हूं?

public class MainActivity extends AppCompatActivity implements LocationListener { 

    LocationManager locationManager; 
    String provider; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     provider = locationManager.getBestProvider(new Criteria(), false); 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     Location location = locationManager.getLastKnownLocation(provider); 

     if (location != null) { 

      Log.i("Location Info", "Location achieved!"); 

     } else { 

      Log.i("Location Info", "No location :("); 

     } 

    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.removeUpdates(this); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Double lat = location.getLatitude(); 
     Double lng = location.getLongitude(); 

     Log.i("Location info: Lat", lat.toString()); 
     Log.i("Location info: Lng", lng.toString()); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

    public void getLocation(View view) { 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     Location location = locationManager.getLastKnownLocation(provider); 

     onLocationChanged(location); 


    } 

} 

उत्तर

40

आपको वास्तव में रनटाइम पर स्थान अनुमति का अनुरोध करने की आवश्यकता है (यह बताते हुए आपके कोड में टिप्पणियां देखें)।

स्थान अनुमति का अनुरोध करने के लिए यहां परीक्षण और कार्य कोड है।

import android.Manifest; 

फिर गतिविधि में इस कोड डाल:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public boolean checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an explanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      new AlertDialog.Builder(this) 
        .setTitle(R.string.title_location_permission) 
        .setMessage(R.string.text_location_permission) 
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          //Prompt the user once explanation has been shown 
          ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
            MY_PERMISSIONS_REQUEST_LOCATION); 
         } 
        }) 
        .create() 
        .show(); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // location-related task you need to do. 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

        //Request location updates: 
        locationManager.requestLocationUpdates(provider, 400, 1, this); 
       } 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 

      } 
      return; 
     } 

    } 
} 

फिर onCreate() में checkLocationPermission() विधि कॉल:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //......... 

    checkLocationPermission(); 
} 

फिर आप

android.Manifest आयात करने के लिए सुनिश्चित करें onResume() औरका उपयोग करेंठीक है क्योंकि यह सवाल में है।

यहाँ एक संक्षिप्त संस्करण में थोड़ा और अधिक साफ है कि है:

@Override 
protected void onResume() { 
    super.onResume(); 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 

     locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 

     locationManager.removeUpdates(this); 
    } 
} 
+0

आदमी तुम इतनी मच का शुक्र है, सब कुछ काम करता है! आप राजा हैं! – bojan

+0

'प्रदाता' क्या है? और 'मुख्य गतिविधि' गतिविधि है जिसकी अनुमति से अनुरोध किया जाता है? –

+1

@AbdullahUmer आप देख सकते हैं कि प्रश्न में 'प्रदाता' का उपयोग कैसे किया जाता है: 'स्ट्रिंग प्रदाता = स्थान प्रबंधक .getBestProvider (नया मानदंड(), झूठा); ' –

संबंधित मुद्दे