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

Android实现打电话功能

2013-05-30 20:38 281 查看

初学安卓,入门的应用 。打电话。

新建Android 项目



layout 控件布局 :activity_main.xml

<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/phoneview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lable_phone" />

<EditText
android:id="@+id/editview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phoneview"
android:layout_below="@+id/phoneview"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="phone" >

<requestFocus />
</EditText>

<Button
android:id="@+id/butphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editview"
android:layout_below="@+id/editview"
android:layout_marginTop="16dp"
android:text="@string/butphone" />

</RelativeLayout>

在AndroidManifest.xml 文件 中为其添加 打电话的 权限





你会在你发现在AndroidManifest.xml 文件 中有了一断这样的代码 其为打电话的 权限

//设置拨打电话 的权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission />

上面已经布局好 了。下面

然后在 MainActivity 中实现其功能:

public class MainActivity extends Activity {
private EditText editText;
private Button button_phone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示pylt
setContentView(R.layout.activity_main);

//获取按钮组件
button_phone=(Button) findViewById(R.id.butphone);
//获取输入框组件
editText = (EditText) findViewById(R.id.editview);

//注册按钮事件
button_phone.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取号码
String phone_Num = editText.getText().toString();

//定义执行打电话的意图对象
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone_Num));

//执行意图
MainActivity.this.startActivity(intent);

//吐司的效果
Toast.makeText(MainActivity.this, "正在给"+phone_Num+"打电话",Toast.LENGTH_LONG).show();

}
});

}

}

然后把你写好的项目 发布到你的虚拟机上,Ok .



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