Hello everybody.I want some advice,i make a program on Android and i wanto to do this,i want to run the app on my real device wich is attach on Android Studio,show my contact list,and when i push a number to start calling.
This is my code
Thank you very much for any sugestions
This is my code
Java:
public class MainActivity extends AppCompatActivity {
public static final int PICK_CONTACT_REQUEST = 1;
TextView contactNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactNumber = (TextView) findViewById(R.id.contactnumber);
Button buttonPickContact = (Button) findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickContact();
}
});
}
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
//==========================================================================================
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
Uri contactUri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
//HERE IS THE PROBLEM,WHEN I HIT A NUMBER IT RETURNS TO THE STARTING SCREEN
//I TRY TO DO WITH INTENTS
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(number));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
startActivity(intent);
}
}
}
}
Thank you very much for any sugestions