当前位置: 代码迷 >> Android >> Android札记-短信与拨号
  详细解决方案

Android札记-短信与拨号

热度:54   发布时间:2016-05-01 15:43:06.0
Android笔记--短信与拨号
1.使用Android自带的拨号功能简单实现:	(1)。首先在AndroidManifest.xml中添加权限设置:			<uses-permission android:name="android.permission.CALL_PHONE"/>	(2)。在main.xml中设置三个控件:			<TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/call.input"/>			//谁知一个文本控件,显示R类下的String子类的属性为call.input的值,此处值所对应的是 res/values/xxx.xml下的call.input所对应的值						<EditText   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:id="@+id/input"/>			//创建一个输入框,以便用户输入电话号码,并为其分配id为input,此id会在R类的子类id下表现出来						<Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/call.call"   android:id="@+id/call" />			//创建一个拨号按钮,以便用户拨打输入的电话号码,并为其分配id为call,此id会在R类的子类id下表现出来	(3)。在Activity中编写如下代码:			public class HelloActivity extends Activity {   				private EditText telText;								/** 当应用初始化的时候执行,安装的时候执行,使用的时候不再执行,只执行一次. */				@Override				public void onCreate(Bundle savedInstanceState) {					super.onCreate(savedInstanceState);	//调用父类的onCreate()方法,不能省略;					setContentView(R.layout.main);		//将视图显示为R类下的子类layout中的属性为main所引用的xml,一般是指(一般不做改变):res/layout/main.xml										telText = (EditText) this.findViewById(R.id.input);		//通过id得到输入框对象					Button call = (Button) this.findViewById(R.id.call);		//通过id得到按钮对象										call.setOnClickListener(new CallListener());			//为按钮对象添加点击事件									}			    				/** 创建事件处理接口的实现类 */				private final class CallListener implements View.OnClickListener {						@Override					public void onClick(View v) {						String telNum = telText.getText().toString();		//得到输入框中输入的文本值						if(telNum == null || "".equals(telNum)) {							return;						}												Intent intent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + telNum));						//*****此处的android.intent.action.CALL,可以使用常量 Intent.ACTION_CALL 代替						//*****   new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNum));												//添加一个目的(也可以叫意图),此处引用的是Android自带的拨号意图,它需要三个条件(可在源码中查到)						//1.android.intent.action.CALL  :  action,需要调用的action的类别						//2.数据格式必须遵从“ tel: ”开头(因为源码是:  <data android:scheme="tel"/>)						//3.必须增加权限 <uses-permission android:name="android.permission.CALL_PHONE"/>						//4.还有一个默认的类别,当调用startActivity(intent)的时候系统会自动添加进去;						startActivity(intent);						//执行意图所匹配的Action					}								}			    			}//注意: 以上的接口实现类也可以通过匿名内部类来实现2. 使用Android发送短信:	(1). 首先在AndroidManifest.xml中添加权限设置:			<uses-permission android:name="android.permission.SEND_SMS"/>	(2). 设置控件:			<?xml version="1.0" encoding="utf-8"?>			<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"				android:layout_width="fill_parent" android:layout_height="fill_parent">				//控件之间的嵌套, 相对布局下面 嵌套一个线性布局: 左边文本 ,右边输入框				<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" 							  android:id="@+id/layout"	>					<TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/tel_num" 							  android:textSize="20sp" />					<EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="10dip" android:id="@+id/telNum"/>				</LinearLayout>				//创建相对控件 android:layout_below : 相对某个控件的下方  android:layout_toLeftOf : 相对某个控件的左方 。。。。。。				<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" 						  android:textSize="20sp" android:layout_below="@id/layout" android:id="@+id/content_text" />						  				<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/content_text" 						  android:id="@+id/content"	android:lines="3"/>				<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send"						android:layout_below="@id/content"  android:id="@+id/send" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" />										<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel"						android:layout_toLeftOf="@id/send" android:layout_alignTop="@id/send" android:id="@+id/cancel"  />			</RelativeLayout>		(3) 编写Activity代码:						public class MsgSenderActivity extends Activity {											private EditText content;		//定义短信文本内容对象								/** 初始化时执行. */					@Override				public void onCreate(Bundle savedInstanceState) {					super.onCreate(savedInstanceState);					setContentView(R.layout.main);										Button send = (Button) this.findViewById(R.id.send);	//得到发送按钮对象					Button cancel = (Button) this.findViewById(R.id.cancel);	//得到取消按钮对象					content = (EditText) this.findViewById(R.id.content);	//得到短信文本对象										send.setOnClickListener(new View.OnClickListener() {							@Override							public void onClick(View v) {																EditText tel = (EditText) findViewById(R.id.telNum);		//得到需要发送的号码对象								String telNum = tel.getText().toString();				//得到需要发送的电话号码								String contextValue = content.getText().toString();		//得到需要发送的短信内容																if(telNum == null || "".equals(telNum.trim())) {			//如果号码为空,直接返回。									return;								}																	SmsManager smsManager = SmsManager.getDefault();			//得到短信发送管理对象								List<String> msgList = smsManager.divideMessage(contextValue);	//由于每条短信中文字数最多70字,所以需要将发送内容进行分段处理(x<=70字 返回的List的size是1)								for(String msg : msgList) {									smsManager.sendTextMessage(telNum, null, msg, null, null);	//根据每段信息,分别发送。   第一个参数:发送号码, 第二个参数:短信中心号码  第三个参数:发送信息, 第四个参数:是否发送成功, 第五个参数: 对方是否接收成功。								}																Toast toast = Toast.makeText(MsgSenderActivity.this, R.string.success, Toast.LENGTH_LONG);		//创建土司提示对象, 第一个参数,需要显示的Activity对象, 第二个参数: 需要显示的内容, 第三个参数:需要显示的时间长度								toast.setMargin(RESULT_CANCELED, 0.345f);									//设置提示对象显示的位置。 第一个参数: 显示的 横向, 第二个参数 显示的纵向								toast.show();														//显示此提示							}											});										cancel.setOnClickListener(new View.OnClickListener() {							@Override							public void onClick(View v) {								content.setText("");			//将短信的文本制空。							}					});																			    }			}
  相关解决方案