2015-10-25 9 views
10

द्वारा अस्वीकार कर दिया जा सकता है। मैं एक ऐसा एप्लिकेशन बनाने की कोशिश कर रहा हूं जो हर पांच मिनट के बाद उपयोगकर्ता के स्थान अपडेट भेजता है। मुझे लगता है कि मेरा कोड ठीक काम कर रहा है लेकिन मुझे एप्लिकेशन द्वारा उपयोग की जाने वाली अनुमतियों के बारे में एक त्रुटि मिलती है। मुझे पूरा यकीन है कि मैंने मैनिफेस्ट फ़ाइल में अनुमतियां जोड़ दी हैं। क्या कोई मुझे बता सकता है कि क्या गलत है? मेरा कोड यहाँ है।कॉल को अनुमतियां आवश्यक हैं जिन्हें उपयोगकर्ता

MainActivity.java

LocationManager locationManager ; 
String provider; 

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

    // Getting LocationManager object 
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    // Creating an empty criteria object 
    Criteria criteria = new Criteria(); 

    // Getting the name of the provider that meets the criteria 
    provider = locationManager.getBestProvider(criteria, false); 

    if(provider!=null && !provider.equals("")){ 

     // Get the location from the given provider 
     Location location = locationManager.getLastKnownLocation(provider); 
     locationManager.requestLocationUpdates(provider,5*60*1000,0,this); 

     if(location!=null) 
      onLocationChanged(location); 
     else 
      Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show(); 

    }else{ 
     Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onLocationChanged(Location location) { 
    // Getting reference to TextView tv_longitude 
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude); 

    // Getting reference to TextView tv_latitude 
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude); 

    // Setting Current Longitude 
    tvLongitude.setText("Longitude:" + location.getLongitude()); 

    // Setting Current Latitude 
    tvLatitude.setText("Latitude:" + location.getLatitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
} 

}

मैं एक त्रुटि हो रही है के रूप में कॉल अनुमति है जो इन में उपयोगकर्ता द्वारा अस्वीकार कर दिया जा सकता है की आवश्यकता है लाइनों

Location location = locationManager.getLastKnownLocation(provider); 
     locationManager.requestLocationUpdates(provider,5*60*1000,0,this); 

मेरे AndroidManifest थाई की तरह है रों

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

उत्तर

25

कौन सा एसडीके उपयोग करते हैं? यदि आप मार्शमलो का उपयोग करते हैं, तो आपको यह जांचना होगा कि उपयोगकर्ता ने प्रत्येक स्थान कॉल के लिए अनुमति दी है। यदि आप न यह पहले से ही है

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 
               LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION); 
     } 

अनुमति का अनुरोध:

एक नज़र Here.

ले लो तुम कुछ इस तरह करना चाहिए।

अधिक जानकारी के लिए ऊपर दिए गए लिंक को चेक करें।

+0

का संकेत नहीं हो सकता है मैं Android 4.2 और इसके बाद के संस्करण का उपयोग कर रहा है। –

+1

आपको एसडीके 23 का उपयोग करके संकलित करना होगा, कोई अन्य स्पष्टीकरण नहीं है।कृपया जांचें कि आप में ऐप की ग्रेडल फ़ाइल – Shahar

+0

हां आप सही थे! यह एसडीके 23 का उपयोग कर संकलन कर रहा था। धन्यवाद इस समस्या को हल किया। –

13

मेरे कोड का प्रयास करें:

public class MainActivity extends AppCompatActivity { 

    /* GPS Constant Permission */ 
    private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11; 
    private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 12; 

    /* Position */ 
    private static final int MINIMUM_TIME = 10000; // 10s 
    private static final int MINIMUM_DISTANCE = 50; // 50m 

    /* GPS */ 
    private String mProviderName; 
    private LocationManager mLocationManager; 
    private LocationListener mLocationListener; 

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

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

     // Get the best provider between gps, network and passive 
     Criteria criteria = new Criteria(); 
     mProviderName = mLocationManager.getBestProvider(criteria, true); 

     // API 23: we have to check if ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION permission are granted 
     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
       || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 

      // No one provider activated: prompt GPS 
      if (mProviderName == null || mProviderName.equals("")) { 
       startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
      } 

      // At least one provider activated. Get the coordinates 
      switch (mProviderName) { 
       case "passive": 
        mLocationManager.requestLocationUpdates(mProviderName, MINIMUM_TIME, MINIMUM_DISTANCE, this); 
        Location location = mLocationManager.getLastKnownLocation(mProviderName); 
        break; 

       case "network": 
        break; 

       case "gps": 
        break; 

      } 

     // One or both permissions are denied. 
     } else { 

      // The ACCESS_COARSE_LOCATION is denied, then I request it and manage the result in 
      // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
         MY_PERMISSION_ACCESS_COARSE_LOCATION); 
      } 
      // The ACCESS_FINE_LOCATION is denied, then I request it and manage the result in 
      // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       ActivityCompat.requestPermissions(this, 
         new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 
         MY_PERMISSION_ACCESS_FINE_LOCATION); 
      } 

     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSION_ACCESS_COARSE_LOCATION: { 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        // permission was granted 
       } else { 
        // permission denied 
       } 
       break; 

      case MY_PERMISSION_ACCESS_FINE_LOCATION: { 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        // permission was granted 
       } else { 
        // permission denied 
       } 
       break; 
      } 

     } 
    } 
} 

स्रोत: LINK

+1

आप सरणी के भीतर अनुमतियों को पार करने के बाद 'अनुरोध पैरामिशन' को कॉल कर सकते हैं, –

+0

@ थियागोपोर्सीनुला मैं कोशिश करूंगा :) आपके सुझाव के लिए धन्यवाद – eldivino87

+1

यह वास्तव में [अनुमति समूह] (http: // डेवलपर) के कारण कोई फर्क नहीं पड़ता .android.com/intl/pt-br/गाइड/विषयों/सुरक्षा/permissions.html # पर्म-समूह)। जब आपको 'ACCESS_FINE_LOCATION' के लिए अनुमति मिलती है, तो आप स्वचालित रूप से 'ACCESS_COARSE_LOCATION' प्राप्त करते हैं, क्योंकि वे एक ही समूह (' LOCATION') से संबंधित होते हैं। इसके बारे में यहां और देखें: http://developer.android.com/intl/pt-br/training/permissions/requesting.html –

4

यह मेरे लिए काम किया

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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. 
     Toast.makeText(YourService.this, "First enable LOCATION ACCESS in settings.", Toast.LENGTH_LONG).show(); 
     return; 
    } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, listener); 
+1

यह सही है, किसी भी कारण से एंड्रॉइड स्टूडियो एक त्रुटि उठाता है यदि आप यह सटीक नहीं रखते हैं स्थान का अनुरोध करते समय कोड, भले ही आप किसी अन्य कस्टम तरीके से अनुमतियां प्रबंधित करते हैं। – Sca09

2

ये कदम उठाएँ इस

ठीक करने के लिए आप करने की आवश्यकता का गुच्छा रहे हैं मुख्य गतिविधि। जावा

if (ContextCompat.checkSelfPermission(this, 
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
== PackageManager.PERMISSION_GRANTED) {   
locationManager.requestLocationUpdates 
(locationManager.requestLocationUpdates(provider,5*60*1000,0,this); 
}//end of if 

अब आप भी अपने build.gradle अद्यतन करने के लिए

dependencies{ 
------------- //your pre-generated code 
compile 'com.android.support:support-v4:23.0.1' 
} 

this Android.Developers इसके बारे में क्या कहते हैं है की जरूरत है।

और आप एक एमुलेटर उपयोग कर रहे हैं आवेदन सेटिंग्ससे अनुमतियाँ देने के लिए भूल नहीं है, क्योंकि यह के लिए इस तरह

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

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