To enable your application to initiate a phone call, you must set permissions in the manifest file:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.c
om/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.MA
IN" /> - <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>
Now create a new activity with a call method. It should look like this:
- 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 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:
- 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;@Overridepublic 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);}}}
0 komentar:
Post a Comment