Android学习笔记(三) 之模拟打电话

首先如图:


新建一个Android工程,基于Anrdoid2.2模拟器创建

然后完善string.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, SharpPhoneActivity!</string>
    <string name="app_name">SharpPhone</string>
	<string name="mobile">请输入手机号</string>
    <string name="button">拨打此号</string>
</resources>

然后是完善界面 修改main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/mobile" />
	<EditText   
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phoneno" />
	<Button 
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/button"
        android:id="@+id/button"
	    />
</LinearLayout>

接着需要在Activity类处理信息

package com.sharpanroid.phone;

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 SharpPhoneActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bt=(Button)this.findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText phoneText=(EditText)findViewById(R.id.phoneno);
				/**
				 * 获取手机号
				 */
				String phoneno=phoneText.getText().toString();
				if(null!=phoneno&&!"".equals(phoneno.trim())){
					/**
					 * 数据通过Uri包封装 ,创建一个拨号意图
					 */
					Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneno));
					
					startActivity(intent);
				}
			}
		});
        
    }
}
由于调用了打电话的服务,需要在AndroidManifest.xml 文件中进行引用

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

    <uses-sdk android:minSdkVersion="8" />
	<uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SharpPhoneActivity"
            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>

该功能看似简单,但也是基础,本文只为自己所积累。日后回看带来些帮助

下载地址为:

点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cuiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值