您的位置:首页 > 其它

安卓实现拨号功能

2013-12-03 12:32 260 查看
学习安卓几天,记录下我的学习点滴!

为什么系统本生就已经带了拨号功能我们还要去实现,因为在某些网页上,比如有个联系人资料,下方有个电话号码,我们直接点击就可以进行拨号!不然的话我们还要把电话号码记录下来然后进行拨号,这样做是不是太麻烦。
1:界面实现,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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phonenumber"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phoneValue"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phoneValue"
android:layout_below="@+id/phoneValue"
android:layout_marginTop="14dp" />

</RelativeLayout>
2:MainActivity.java
package com.example.phone;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
text = (EditText)findViewById(R.id.phoneValue);
button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
String number = text.getText().toString();
Intent intent = new Intent();
//激活源代码,添加intent对象,
intent.setAction("android.intent.action.CALL");
//intent.addCategory("android.intent.category.DEFAULT");内部会自动添加类别,可有可无
intent.setData(Uri.parse("tel:"+number));
//激活Intent
startActivity(intent);

}

});
}

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phone.MainActivity"
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>
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
</manifest>
在MainActivity中必须用意图对象调用系统的拨号功能,参照MainActivity代码,还有必须为你的拨号功能进行权限配置,不然拨号功能系统是不会处理的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: