1

AI have made a simple app that displays a marker at user's current location and I have used all permissions in the manifest. Here is the code:

public class LocationActivity extends FragmentActivity implements LocationListener {
SupportMapFragment map;
GoogleMap googleMap;
LocationManager loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    map=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    onMapReady(googleMap);
    //onLocationChanged(location);
}
public void onMapReady(GoogleMap googleMap){
    try {
        googleMap = map.getMap();
        if (googleMap != null) {
            googleMap.setMyLocationEnabled(true);
            loc = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria crit = new Criteria();
            String best = loc.getBestProvider(crit,true);
            Location location = loc.getLastKnownLocation(best);
            LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15));
            googleMap.addMarker(new MarkerOptions().title("You're here").position(myLocation));
        } else {
            Toast.makeText(this, "Fragment has a problem", Toast.LENGTH_SHORT).show();
        }
    }
    catch (Exception e){
        Toast.makeText(this,"we can't do it",Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_location, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@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
}

@Override
public void onLocationChanged(Location location) {
//something will go on here
}}

But it shows a runtime error:

ACCESS_FINE_LOCATION is required for PRIORITY_HIGH_LOCATION_REQUEST

What is the probable reason here? Although the code compiles well if I remove this line -

googleMap.setMyLocationEnabled(true);

But the location is not accurate.` Here is the manifest file

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="API_Key"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <activity
        android:name=".LockScreenActivity"
        android:label="@string/title_activity_lock_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LocationActivity"
        android:label="@string/title_activity_location" >
    </activity>
</application>

Neeraj Verma
  • 703
  • 6
  • 15

2 Answers2

1

put All below snippet outside application tag

 <android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

And also add your package name at the time of adding permission

<permission android:name="yourpackagename.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="yourpackagename.permission.MAPS_RECEIVE" />
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
1

Two problems:

  1. All of your <uses-permission>, <permission>, and <uses-feature> elements need to move outside of <application>. They should be immediate children of the root <manifest> element.

  2. You do not use the android: prefix on element names, as you have with READ_PHONE_STATE and READ_EXTERNAL_STORAGE. You do not need READ_EXTERNAL_STORAGE anyway, as you are requesting WRITE_EXTERNAL_STORAGE.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491