android 开发电话拔号
第一步新建一个名为phone的android项目
第二步画用户页面,我们需要一个TextVIew EditVIew Button 这三个组件,部局现在使用线性部局(linerLayout)具体内容如下。
main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/phone" /> <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout>
第三步:需要在页面展示的提示内容,我在values文件夹下新建了一个me.xml文件,格式如下:
me.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="phone">请输入号码</string> <string name="button">播号</string> </resources>
内容会在R.java 文件下自动生成对应的引用。
第四步:把需要的logo 文件放到drawable-hdpi下,注:有时候直接放入会出错,如果出错,请用mspaint 打开再保存成png 格式放入。
第五步:也是最重要的一步开始编码,实现我们需求的拔号功能, 但不是我们全部开发,是调用android已实现的拔号功能。
package com.hkrt.action; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class PhoneActivity extends Activity { /** Called when the activity is first created. */ private EditText phone;//获取电话号码 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); phone = (EditText) findViewById(R.id.phone); Button bu = (Button)findViewById(R.id.button); bu.setOnClickListener(new buttonListener()); } public final class buttonListener implements View.OnClickListener{ // Intent.ACTION_DIAL @Override public void onClick(View v) { String phoneNum= phone.getText().toString(); Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel://" + phoneNum)); startActivity(intent); } } }
第六步:给用户添加拔号权限;在AndroidMainifest.xml中添加。
<uses-permission android:name="android.permission.CALL_PHONE"/>
同时把我们需要的小图标Logo加上去,
android:icon="@drawable/zs"
最终项目图:
最后的页面:
两个不同的拔号结果:
两个参数的不同,出来的结果名异。