Advertisemnet

Earn Extra Money At Home. Starting from Now!!!

Monday, July 21, 2014

Make a phone call from your android application

Set permissions in the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bubudsadasdas"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon"android:label="@string/app_name">
        <activity android:name=".phonecalls"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
</manifest>

Create a new activity

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 e) {
        Log.e("helloandroid dialing example", "Call failed", e);
    }
}

What happens when you begin a telephone call depends, partially, on the phone system. The number may be inaccurate. The system may be occupied or generally inaccessible. The call could be interfered. Here, on the other hand, you see no failure taking care of rationale, aside from discovering and logging special cases that might be tossed if slip experiences an issue when discovering applications that can prepare Intent articles.

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