`
1140566087
  • 浏览: 548734 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18106
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309907
Group-logo
J2ME 基础学习课程集
浏览量:18086
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17221
社区版块
存档分类
最新评论

Android 之 自动拨号

阅读更多
介绍:
在自己的小应用中,得到了某些号码,并且希望通过该号码进行拨号,则可以保存当前的
数据,并带到拨号界面,进行拨号;因此诞生了以下代码;


主要核心代码:
package com.sun.callphone;

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

public class MainActivity extends Activity {

	//声明对象
	private EditText phonenumber ;
	private Button call,show;
	
	
	// 程序入口
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//获取对象
		phonenumber = (EditText) findViewById(R.id.phonenumber);
		call = (Button) findViewById(R.id.call);
		show = (Button) findViewById(R.id.show);
		
		//点击事件触发 -- 根据用户输入的电话调转到拨号的界面
		call.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				//第一步:获取用户输入数据
				 String number = phonenumber.getText().toString();
				
				// 第二步:首先判断用户输入的号码是否合乎规范
				if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
					
					//第三步:注意权限的使用  
					//<!-- 获取拨打电话的权限 -->
					//<uses-permission android:name="android.permission.CALL_PHONE" />
					
					//第四步:开始拨打
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_CALL); // 隐式意图,指定动作
					intent.setData(Uri.parse("tel:"+number)); // 带值
					startActivity(intent);	//开始调转
				}else{
					Toast.makeText(MainActivity.this, "请重新输入号码!"+number, 2000).show();
				}
			}
		});
		
		//显示源码
		show.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction("sun.show");
				startActivity(intent);
				
			}
		});
	}
}

布局文件:

<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" >

    <EditText
        android:id="@+id/phonenumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="input number...."
        android:inputType="number" />

    <Button
        android:id="@+id/call"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/phonenumber"
        android:layout_alignParentBottom="true"
        android:text="拨打测试效果" />

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/call"
        android:layout_alignLeft="@+id/call"
        android:layout_alignRight="@+id/call"
        android:layout_marginBottom="55dp"
        android:text="查看核心代码及说明" />

</RelativeLayout>


主配置文件:


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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon_world"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sun.callphone.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>

</manifest>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics