实例非常简单,意在体验Android的Intent,用户权限。
Intent 见 http://blog.csdn.net/zengmingen/article/details/49586045
用户权限 见 http://blog.csdn.net/zengmingen/article/details/49586569
-----------------------------------------------------------------------------------------------
MainActivity.java
package com.example.callPhone;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;public class MainActivity extends Activity {private EditText et;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et=(EditText) findViewById(R.id.phoneNumber);}public void callPhone(View v){String phoneNumber=et.getText().toString();//拨打电话是系统应用,谷歌没有提供直接的打电话API,需要通过意图调用//创建意图对象Intent intent=new Intent();//我们需要告诉系统,我们的动作:我要打电话intent.setAction(Intent.ACTION_CALL);//把电话号码告诉系统的打电话应用。这是android系统设计思想,组件调用intent.setData(Uri.parse("tel:" + phoneNumber));startActivity(intent);//现实中,打电话要钱,所以需要增加打电话权限}}
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.callPhone"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.CALL_PHONE"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.callPhone.MainActivity"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></manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/inputNumber" /><EditTextandroid:id="@+id/phoneNumber"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="phone"/><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/callPhone"android:onClick="callPhone"/></LinearLayout>
代码下载:
http://download.csdn.net/detail/zengmingen/9172881