1

I'm writing android app with Android Build Target API 24 (OS 7). I have to prompt permission of ACCESS_COARSE_LOCATION for API 23 & above versions. So I change my Android.Manifest like below.

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="24" />

After that My app only show white screen and nothing happen. I'm using with Eclipse IDE. But when I change the targetSdkVersion to 22 then the app working fine in other process but doesn't show the Permission Prompt for ACCESS_COARSE_LOCATION and the location process cannot use and the below error show in LogCat.

Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results

How should I do that? Please help me. Do I need to change targetSdkVersion to 24?

Updated

Here is my Manifest.xml Full Code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company_name.apps.my_app_name"
android:versionCode="16"
android:versionName="2.1.1" android:installLocation="internalOnly">

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="24" />


<uses-permission android:name="com.android.vending.BILLING" />

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

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- end GCM permission -->


<!-- map -->

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Here is my Access Permission Code.

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

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        Log.d("PermissionGAndroid6", "MarshMallow OS6");
        if (this.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.d("PermissionG1","Permission Granted Prompt");
            requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
        }
    }
    else {
        setContentView(R.layout.activity_main);

        Exchanger.start(this, APPLICATION_CODE, ORIENTATION, TEST_MODE);
          // default first fragment is MainFragment
          if (findViewById(R.id.container) != null) {
              mMainFragment = new MainFragment();
              FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
              ft.add(R.id.container, mMainFragment, "MainFragment").commit();
          }
          LogTracker.getTracker().startUsingApp();
    }

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[],
                                       int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("PermissionG", "coarse location permission granted");
                setContentView(R.layout.activity_main);

                Exchanger.start(this, APPLICATION_CODE, ORIENTATION, TEST_MODE);
                  // default first fragment is MainFragment
                  if (findViewById(R.id.container) != null) {
                      mMainFragment = new MainFragment();
                      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                      ft.add(R.id.container, mMainFragment, "MainFragment").commit();
                  }
                  LogTracker.getTracker().startUsingApp();
            } else {
                requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);

            }
            return;
        }
    }
}
Kawazoe Kazuke
  • 175
  • 6
  • 20
  • add permissions in the manifest and include runtime permissions – Manoj Perumarath Apr 05 '17 at 04:27
  • Can you show your full manifest? And the runtime permission code? – OneCricketeer Apr 05 '17 at 04:28
  • I updated my Question. Please check and help me. For now problem is after i changed android:targetSdkVersion to "23" or "24", my app only show white screen. On that stage the permission prompt is show. After i change android:targetSdkVersion to below "23" (Eg: "22") Then App is open but Location Access Permission prompt not showing and Permission deinial show in LogCat. – Kawazoe Kazuke Apr 05 '17 at 04:46

3 Answers3

1

Try this,

    private Context mContext=YourActivity.this;

    private static final int REQUEST = 112;


    if (Build.VERSION.SDK_INT >= 23) {
        String[] PERMISSIONS = {android.Manifest.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_FINE_LOCATION};
        if (!hasPermissions(mContext, PERMISSIONS)) {
            ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
        } else {
            //do here
        }
    } else {
         //do here
    }

get Permissions Result

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        //do here
                } else {
                    Toast.makeText(mContext, "The app was not allowed to access your location.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

check permissions for marshmallow

    private static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

Manifest

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Komal12
  • 3,340
  • 4
  • 16
  • 25
0

Above API 22 as per the new android guidelines you need to add permission for each sensitive user data refer https://developer.android.com/training/permissions/requesting.html for learning about the Permission model. And https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html is the example for asking Location Permission above API 22.

Wilson Christian
  • 650
  • 1
  • 6
  • 17
0

app only show white screen and nothing happen.

Please set content view first. You can check and request permission after onCreate() is done.

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

Rust Fisher
  • 331
  • 3
  • 12
  • I changed it what you said. But still White Screen happen :( – Kawazoe Kazuke Apr 05 '17 at 05:13
  • Sorry to hear that. My suggestion is : try `ActivityCompat.requestPermissions` instead of `requestPermissions`. And make sure your UI works well in API24. – Rust Fisher Apr 05 '17 at 05:54
  • I changed to ActivityCompat.requestPermissions . On that time ActivityCompat cannot be resolved. – Kawazoe Kazuke Apr 05 '17 at 06:19
  • With Android Studio and gradle, use `compile 'com.android.support:appcompat-v7:25.3.0'` . So I suggest you try to import v4 or v7 appcompat in Eclipse. – Rust Fisher Apr 05 '17 at 06:26
  • I added v4 library but it's still showing => The method requestPermissions(String[], int) is undefined for the type ActivityCompat – Kawazoe Kazuke Apr 05 '17 at 07:31
  • Em... Did your v4 lib really be imported? I'm not using Eclipse, please see this http://stackoverflow.com/questions/24998368/the-import-android-support-v7-cannot-be-resolved – Rust Fisher Apr 05 '17 at 07:57
  • If you already imported v4 lib, make sure these method work. https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html – Rust Fisher Apr 05 '17 at 10:24