Android Permission

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);
}



2 Comments

  1. This is a very good tips especially to those new to blogosphere, brief and accurate information… Thanks for sharing this one. A must read article.

  2. I simply want to mention I am newbie to weblog and definitely loved this web site. Very likely I’m planning to bookmark your blog post . You really come with impressive article content. Cheers for sharing your webpage.

Leave a Reply

Your email address will not be published.


*