Android Studio Learning (vi) content provider

Source: Internet
Author: User

Run-time permissions
    • Use Contextcompat.checkselfpermission (mainactivity.this,manifest.permission.call_phone)! = packagemanager.permission_granted//determine if there is permission
    • Activitycompat.requestpermissions (Mainactivity.this,
      New string[] {manifest.permission.call_phone},1);//no permission to apply
    • Onrequestpermissionresult ()//Any result will be callback to this function, so rewrite this function

Makecallbutton.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
if (contextcompat.checkselfpermission (Mainactivity.this, Manifest.
Permission. Call_phone)!=packagemanager.permission_granted)
Activitycompat.requestpermissions (MainActivity.this,
New String[] {manifest.permission.call_phone},1);
Else
Call ();
}
});
private void Call ()
{
try{
Intent Intent = new Intent (intent.action_call);
Intent.setdata (Uri.parse ("tel:10086"));
StartActivity (Intent);
}
catch (SecurityException e)
{
E.printstacktrace ();
}
}

@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {    switch (requestCode){    case 1:        if(grantResults.length >0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)            call();        else            Toast.makeText(MainActivity.this,"you denied the  permission",Toast.LENGTH_LONG).show();        break;    default:}}

}

Find contact name and phone

ListView Contactsview = (ListView) Findviewbyid (R.id.listview);
adapter = new Arrayadapter

private void Readcontanct ()
{
cursor cursor = NULL;
try{
cursor = Getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
Null,null,null,null);
if (cursor! = NULL)
while (Cursor.movetonext ()) {
String displayName = cursor.getstring (cursor.getcolumnindex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number =cursor.getcolumnname (cursor.getcolumnindex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contactslist.add (number);
Adapter.notifydatasetchanged ();
}
}
catch (Exception e)
{
E.printstacktrace ();
}
finally {
if (cursor!=null)
Cursor.close ();
}
}

Android Studio Learning (vi) content provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.