Apr 21, 2012

How to make a phone call from your application


To enable your application to initiate a phone call, you must set permissions in the manifest file:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.bubudsadasdas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon"android:label="@string/app_name">
  7.         <activity android:name=".phonecalls"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.     </application>
  15.     <uses-sdk android:minSdkVersion="3" />
  16. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  17. </manifest>
Now create a new activity with a call method. It should look like this:
  1. private void call(String num) {
  2.     try {
  3.         Intent callIntent = new Intent(Intent.ACTION_CALL);
           num="tel:"+num;
  4.         callIntent.setData(Uri.parse(num));
  5.         startActivity(callIntent);
  6.     } catch (ActivityNotFoundException e) {
  7.         Log.e("helloandroid dialing example""Call failed", e);
  8.     }
  9. }
What happens when you start a phone call depends, in part, on the telephone network. The number may be incorrect. The network may be busy or otherwise unavailable. The call can be interrupted. Here, however, you see no error-handling logic, except for catching and logging exceptions that can be thrown if error encounters a problem when finding applications that can process Intent objects.
Now call the call :) method in an empty activity:

  1. import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class phonecalls extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private EditText mEtNum;
    private Button mBtnCall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mEtNum = (EditText) findViewById(R.id.xEtNumber);
    mBtnCall = (Button) findViewById(R.id.xBtnCall);
    mBtnCall.setOnClickListener(this);
    }

    public void onClick(View v) {
    String num = mEtNum.getText().toString();
    if (num.length() > 5) {
    call(num);
    } else {
    Toast.makeText(getApplicationContext(), " Wrong Number ", 1).show();
    }
    }

    private void call(String num) {
    try {
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    num = "tel:" + num;
    callIntent.setData(Uri.parse(num));
    startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
    Log.e("dialing-example", "Call failed", activityException);
    }
    }

    }




No comments:

Post a Comment