Android拨号器的实现

Android自带了拨号功能和拨号器,但是在很多的应用中,需要在自己的应用中集成拨号的功能,方便客户直接点击

就可以完成打电话,所以这样的调用Android拨号器的功能还是非常有实用价值的,下面我们来介绍一下Android拨号器

的实现。

调用Android自带的拨号功能其实我们是使用满足他的意图对象而实现调用的,下面我们首先来看一下主要的

Activity代码

package com.bird.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 PhoneActivity extends Activity {
	private EditText text;//寻找空间这种事情只需要做一次就够了
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText) findViewById(R.id.mobile);//得到文本输入框
        Button button = (Button) this.findViewById(R.id.button);//根据id寻找控件
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String mobileText = text.getText().toString();
				Intent intent = new Intent();//创建一个意图对象,用来激发拨号的Activity
				intent.setAction("android.intent.action.CALL");
				intent.setData(Uri.parse("tel:"+mobileText));
				startActivity(intent);//方法内部会自动添加类别,android.intent.category.DEFAULT
			}
		});//添加点击事件 
    }
    /*
    //创建内部类实现回调对象
    private final class ButtonClick implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			
			String mobileText = text.getText().toString();
			Intent intent = new Intent();//创建一个意图对象,用来激发拨号的Activity
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:"+mobileText));
			startActivity(intent);//方法内部会自动添加类别,android.intent.category.DEFAULT
		}
    	
    }*/
}


大部分介绍都放在了代码的注释上面,其实还是很简单的。

然后最主要的就是配置文件了

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bird.phone"
    android:versionCode="1"
    android:versionName="1.0" >
    
     <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
     <uses-permission android:name="android.permission.CALL_PHONE"/>
     
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PhoneActivity"
            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>

主要是权限问题
<uses-permission android:name="android.permission.CALL_PHONE"/>

调用拨号功能需要有权限调用拨号,这个东西会在用户安装这个应用的时候提示你是否允许他使用拨号的功能,好,下面就是一个
界面的问题了
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:inputType="text"
        android:id="@+id/mobile"
       />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"
        />
</LinearLayout>

然后是strings.xml


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

    <string name="hello">Hello World, PhoneActivity!</string>
    <string name="app_name">Phone</string>
	<string name="mobile">请输入手机号</string>
	<string name="button">拨号</string>
</resources>

好,至此一个简单的Android拨号器就完成了,功能虽小,但是使用的地方还是很多的.

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值