Android Permission

Spread the love

Protects Resources & Data

What are Permissions?

  • Android Protects Resources & Data with Permissions.
  • Normally Used to :
  • Limit Access to User Information (Eg: Contacts)
  • Limit Access to Cost Sensitive APIs (Eg: SMS/MMS)
  • Limit Access to System Resources (Eg: Camera )
  • Permissions are represented as Strings.
  • Declare in the Manifest.xml

 

Use of Permissions

App specify the permission in a special tag called “<uses-permission>” in Manifest File.

Eg:
<uses-permission android:name=“android.permission.READ_CONTACTS”>
</uses-permission>

 

 

Custom Permission ( Use)

That custom permission must be declared in any app that should launch.

<uses-permission
android:name=“com.im.SPLASH_PERMISSION” >

</uses-permission>

More Permissions

  • Component Permission
  • Service Permission
  • Broadcast Receiver Permission
  • Content Provider Permission

Send SMS

  • Check Runtime Permissions

if (ContextCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
  if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.SEND_SMS)) {
  } else {
     ActivityCompat.requestPermissions(this,
      new String[]{Manifest.permission.SEND_SMS}, 1);
  }
} else{
  sendSMS();
}

Handle if Granted

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 if(requestCode==1 && grantResults.length > 0
          && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            sendSMS();
     }
}

Read Contacts

Open Contacts

Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactIntent, PICK_CONTACT);

Permission

<uses-permission android:name=”android.permission.READ_CONTACTS”/>

 

Read & Retrieve Contact Data


@Override
protected void onActivityResult(int reqCode, int resCode, Intent intent) {
if(reqCode == PICK_CONTACT && resCode == Activity.RESULT_OK){
Uri data = intent.getData();
Cursor c = getContentResolver().query(data,
null, null, null, null);
if(c.moveToFirst()){
String name =c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id =
c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

         }
      }
}

Read & Retrieve Phone Numbers from Contact Data

Cursor numbers =
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +” = “+ id, null, null);
if(numbers.moveToFirst()){
String mobile =
numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
etMobile.setText(mobile);
}



About Chandana Nalin Cooray 75 Articles
Chandan is a tech enthusiast and digital strategist with over 5 years of experience in Artificial Intelligence and Software development. He specializes in simplifying complex tech trends for global audiences. When he’s not reviewing the latest AI tools, he shares his insights on future technology on chandanai.com. Follow him on LinkedIn for more tech updates