`

ACTION.CALL

 
阅读更多

      通过一个Button来实现打电话  为了避免用户输入非电话号码的字符串,再拨打电话之前通过自定义isPhoneNumberVaild()以及Toast信息来提示用户

   定义一个Activity

 

package cn.mw.com.phone;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class EX01_02Activity extends Activity {
	EditText ed_phone;
	Button call_phone;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ed_phone = (EditText) findViewById(R.id.phone_call);
		call_phone = (Button) findViewById(R.id.call_btn);
		call_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strInput = ed_phone.getText().toString();
				try {
					if (isPhoneNumberValid(strInput) == true) {

						// 穿件一个新的Intent 运行ACTION.CALL的常数与通过uri将字符串带入

						Intent myIntentDial = new Intent(
								"android.intent.action.CALL", Uri.parse("tel:"
										+ strInput));
						//在StartActivity()方法里面带入自定义的Intent对象以运行拨打电话的工作
						startActivity(myIntentDial);
						ed_phone.setText("");
						
					}else{
						ed_phone.setText("");
						Toast.makeText(getApplicationContext(), "输入的电话号码格式不符", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	protected boolean isPhoneNumberValid(String phoneNumber) {
		boolean isValid=false;
		String expression="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
		String expression2="^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
		CharSequence inputStr=phoneNumber;
		//创建Patten
		Pattern pattern=Pattern.compile(expression);
		//将Patten以参数传入Matcher作 Regular  expression
		Matcher matcher=pattern.matcher(inputStr);
		//创建Patten2
		Pattern pattern2=Pattern.compile(expression2);
		//将Patten2以参数传入Matcher作 Regular  expression
		Matcher matcher2=pattern.matcher(inputStr);
		if(matcher.matches()||matcher2.matches()){
			isValid=true;
		}
		// TODO Auto-generated method stub
		return isValid;
	}
}

 定义一个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" >
<EditText 
    android:id="@+id/phone_call"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入电话号码"
    />
<Button 
    
    android:id="@+id/call_btn"
    android:layout_width="140dip"
    android:layout_height="wrap_content"
    android:text="拨打电话"
    />
</LinearLayout> 
 

 

然后再在EX01_02mainfest.xml里边添加

 <uses-permission android:name="android.permission.CALL_PHONE"/>

 

综上所述就完成了打电话的时候防止出现不是电话号码的情况以便于提醒用户

分享到:
评论

相关推荐

    android Action call 拨打电话 Intent.ACTION.CALL

    android Action call 拨打电话 Intent.ACTION.CALL

    AutoPhoneCall-master.zip

    自动拨号APP,感兴趣的... call = os.popen('adb shell am start -a android.intent.action.CALL -d tel:{}'.format(number)) sleep(20) #挂断电话 Hangup = os.popen('adb shell input keyevent 26') sleep(5)

    android实现打电话功能

    解压就可运行。... //"android.intent.action.CALL" Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile)); startActivity(intent);//内部会添加android.intent.category.DEFAULT

    android 学习笔记

    intent.setAction("android.intent.action.Call"); intent.setData(Uri.parse("tel:"+i)); startActivity(Intent); 权限: &lt;uses-permission android:name="android.permission.CALL_PHONE"/&gt; 08:短信发送 ...

    package com.example.f1_telephone;

    phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + str)); AlertDialog dialog=new AlertDialog.Builder(this).setTitle("请选择服务项目").setItems(new String[]{"拨打电话","发送...

    OutgoingCallBroadcaster.rar_Call to Action

    OutgoingCallBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the ACTION_NEW_OUTGOING_CALL intent which allows other applications to monitor, redirect, or prevent the outgoing call.

    swarm 的帮助文档

    ActionCall - interface swarm.activity.ActionCall. An action defined by calling a C function.. ActionCallC - interface swarm.activity.ActionCallC. .........................................................

    基于Android CALL SendMes Test的相关介绍

    Intent:当找到与设置的Intent限制相同的Activity时候通过startActivity()就会启动该Activity电话播号器:intent.setAction(“android.intent.action.CALL”);intent.addCategory(“android.intent.category.DEFAULT”...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机震动的...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机震动的...

    这个示例通过ACTION_CALL 动作拨打电话

    这是一个拨打电话的示例,本示例中 假设电话号码格式为区号3位 + 号码8位,实际开发可根据需求进行调整

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机震动的...

    android 系统中的几乎所有广播接收者

    CALL_FORWARDING_STATE_CHANGED_ACTION 语音电话的呼叫转移状态已经改变 "android.intent.action.CFF" CONFIGURATION_CHANGED_ACTION 设备的配置信息已经改变,参见 Resources.Configuration. "android.intent....

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机...

    AndroidPhoneDialerCompatLibrary:一个库,用于将格式正确的拨号意图发送到各种Android手机应用程序

    通常,意图是:android.intent.action.DIAL android.intent.action.CALL 通过分析清单,有时是反复试验,您可以确定将电话数据发送到不同应用程序的确切格式。 要将其他电话应用程序支持添加到该库中,所需要做的...

    jquery五角星评分效果代码

    methods.error.call(this, 'you must add the "click: function(score, evt) { }" callback.'); } methods.setTarget.call(this, score, true); }); }, error: function(message) { $(this).html(message); ...

    Google Android SDK开发范例大全的目录

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 自制发送Email程序——Intent在Email上的使用-p165 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 ...

    Google Android SDK开发范例大全(完整版)

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机震动的...

    Google Android sdk 开发范例大全 部分章节代码

    拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 自制发送Email程序——Intent在Email上的使用-p165 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 ...

Global site tag (gtag.js) - Google Analytics