您的位置:首页 > 移动开发 > Android开发

Android学习笔记_1_拨打电话

2013-10-28 22:01 411 查看
1、首先需要在AndroidManifest.xml文件中加入拨打电话的权限,对应的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.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>
<!-- 权限的作用:包含用户信息安全,当用户在安装软件时会得到安全信息提示,用户判断是否继续进行安装 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>

</manifest>


View Code
2、界面布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:gravity="center"
android:textSize="35sp"/>

<EditText
android:id="@+id/editText1"
android:layout_below="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:text="@string/hello_btn" />

</RelativeLayout>


3、Activity类实现,点击事件的实现采用了匿名类的实现方式,

  

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)this.findViewById(R.id.btn);
final EditText phone = (EditText)this.findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String ph = phone.getText().toString();
if(ph!=null && !ph.trim().equals("")){
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
//intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:"+ph));
startActivity(intent);//它内部会自动为intent添加类别,上面的内部可以不用写
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: