How to make a phone-call in Android
Here is the code:
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("phonecall", "Call failed", activityException);
}
}
Manifest file:
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Here is the code after completing the call activity it will return back to your application:
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE);
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// ringing state
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
//active state
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
//idle state
if (isPhoneCalling) {
Intent intent = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent );
isPhoneCalling = false;
}
}
}
}
No comments:
Post a Comment