分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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页面文件如下
- <code class="language-html"><?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></code>
<?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
- <code class="language-html"><?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></code>
<?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拨号器就完成了,功能虽小,但是使用的地方还是很多的.
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)