I learned to define my own custom permission with different level (normal, signature, dangerous) a few hours ago. Then I followed the android documentation and this question's answered by Yury to test it in real. First I made the permission level normal
and after clicking button from first activity, I can access the second activity. But when I set it dangerous
, I failed to access the second activity. I got
Security exception - permission denial
everytime. Then I go to 'Permission' section in the app settings and see my custom permission
is turned off. I turn it on and then can access the activity.
But my question is how I can request this permission like other dangerous permission defined by android system and show dialog and grant it without going to the permission section in app settings. I know how to access permission with -
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// 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.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
But the problem is android's default permissions are defined in Manifest.permission.READ_CONTACTS
but how I define my custom permission with that.
My code is given below.
PermissionTestServerActivity (First activity):-
public class PermissionTestServerActivity extends Activity {
private static final String TAG = "PermissionTestServerActivity";
/** Called when the activity is first created. */
Button btnTest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnTest = (Button) findViewById(R.id.btnTest);
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Button pressed!");
Intent in = new Intent();
in.setAction("com.testpackage.permissiontestclient.MyAction");
in.addCategory("android.intent.category.DEFAULT");
startActivity(in);
}
});
}
}
And its manifest :-
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="com.testpackage.mypermission"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PermissionTestServerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
PermissionTestClient's manifest (Second activity):-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testpackage.permissiontestclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<permission android:name="com.testpackage.mypermission" android:label="my_permission" android:protectionLevel="dangerous"></permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:permission="com.testpackage.mypermission"
android:name=".PermissionTestClientActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter >
<action android:name="com.testpackage.permissiontestclient.MyAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>