Android笔记--短信与拨号

Java代码 复制代码 收藏代码
  1.   
  2. 1.使用Android自带的拨号功能简单实现:   
  3.     (1)。首先在AndroidManifest.xml中添加权限设置:   
  4.             <uses-permission android:name="android.permission.CALL_PHONE"/>   
  5.     (2)。在main.xml中设置三个控件:   
  6.             <TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/call.input"/>   
  7.             //谁知一个文本控件,显示R类下的String子类的属性为call.input的值,此处值所对应的是 res/values/xxx.xml下的call.input所对应的值   
  8.                
  9.             <EditText   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:id="@+id/input"/>   
  10.             //创建一个输入框,以便用户输入电话号码,并为其分配id为input,此id会在R类的子类id下表现出来   
  11.                
  12.             <Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/call.call"   android:id="@+id/call" />   
  13.             //创建一个拨号按钮,以便用户拨打输入的电话号码,并为其分配id为call,此id会在R类的子类id下表现出来   
  14.   
  15.     (3)。在Activity中编写如下代码:   
  16.             public class HelloActivity extends Activity {   
  17.       
  18.                 private EditText telText;   
  19.                    
  20.                 /** 当应用初始化的时候执行,安装的时候执行,使用的时候不再执行,只执行一次. */  
  21.                 @Override  
  22.                 public void onCreate(Bundle savedInstanceState) {   
  23.                     super.onCreate(savedInstanceState); //调用父类的onCreate()方法,不能省略;   
  24.                     setContentView(R.layout.main);      //将视图显示为R类下的子类layout中的属性为main所引用的xml,一般是指(一般不做改变):res/layout/main.xml   
  25.                        
  26.                     telText = (EditText) this.findViewById(R.id.input);     //通过id得到输入框对象   
  27.                     Button call = (Button) this.findViewById(R.id.call);        //通过id得到按钮对象   
  28.                        
  29.                     call.setOnClickListener(new CallListener());            //为按钮对象添加点击事件   
  30.                        
  31.                 }   
  32.                    
  33.                 /** 创建事件处理接口的实现类 */  
  34.                 private final class CallListener implements View.OnClickListener {     
  35.   
  36.                     @Override  
  37.                     public void onClick(View v) {   
  38.                         String telNum = telText.getText().toString();       //得到输入框中输入的文本值   
  39.   
  40.                         if(telNum == null || "".equals(telNum)) {   
  41.                             return;   
  42.                         }   
  43.                            
  44.                         Intent intent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + telNum));   
  45.                         //*****此处的android.intent.action.CALL,可以使用常量 Intent.ACTION_CALL 代替   
  46.                         //*****   new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNum));   
  47.                            
  48.                         //添加一个目的(也可以叫意图),此处引用的是Android自带的拨号意图,它需要三个条件(可在源码中查到)   
  49.                         //1.android.intent.action.CALL  :  action,需要调用的action的类别   
  50.                         //2.数据格式必须遵从“ tel: ”开头(因为源码是:  <data android:scheme="tel"/>)   
  51.                         //3.必须增加权限 <uses-permission android:name="android.permission.CALL_PHONE"/>   
  52.                         //4.还有一个默认的类别,当调用startActivity(intent)的时候系统会自动添加进去;   
  53.   
  54.                         startActivity(intent);   
  55.                         //执行意图所匹配的Action   
  56.                     }   
  57.                    
  58.                 }   
  59.                    
  60.             }   
  61.   
  62. //注意: 以上的接口实现类也可以通过匿名内部类来实现   
  63.   
  64. 2. 使用Android发送短信:   
  65.     (1). 首先在AndroidManifest.xml中添加权限设置:   
  66.             <uses-permission android:name="android.permission.SEND_SMS"/>   
  67.   
  68.     (2). 设置控件:   
  69.             <?xml version="1.0" encoding="utf-8"?>   
  70.             <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"  
  71.                 android:layout_width="fill_parent" android:layout_height="fill_parent">   
  72.   
  73.                 //控件之间的嵌套, 相对布局下面 嵌套一个线性布局: 左边文本 ,右边输入框   
  74.                 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"    
  75.                               android:id="@+id/layout"  >   
  76.   
  77.                     <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/tel_num"    
  78.                               android:textSize="20sp" />   
  79.                     <EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="10dip" android:id="@+id/telNum"/>   
  80.   
  81.                 </LinearLayout>   
  82.   
  83.                 //创建相对控件 android:layout_below : 相对某个控件的下方  android:layout_toLeftOf : 相对某个控件的左方 。。。。。。   
  84.                 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content"    
  85.                           android:textSize="20sp" android:layout_below="@id/layout" android:id="@+id/content_text" />   
  86.                              
  87.                 <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/content_text"    
  88.                           android:id="@+id/content" android:lines="3"/>   
  89.   
  90.                 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send"  
  91.                         android:layout_below="@id/content"  android:id="@+id/send" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" />   
  92.                            
  93.                 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel"  
  94.                         android:layout_toLeftOf="@id/send" android:layout_alignTop="@id/send" android:id="@+id/cancel"  />   
  95.   
  96.             </RelativeLayout>   
  97.        
  98.     (3) 编写Activity代码:   
  99.                
  100.             public class MsgSenderActivity extends Activity {   
  101.                                
  102.                 private EditText content;       //定义短信文本内容对象   
  103.                    
  104.                 /** 初始化时执行. */     
  105.                 @Override  
  106.                 public void onCreate(Bundle savedInstanceState) {   
  107.                     super.onCreate(savedInstanceState);   
  108.                     setContentView(R.layout.main);   
  109.                        
  110.                     Button send = (Button) this.findViewById(R.id.send);    //得到发送按钮对象   
  111.                     Button cancel = (Button) this.findViewById(R.id.cancel);    //得到取消按钮对象   
  112.                     content = (EditText) this.findViewById(R.id.content);   //得到短信文本对象   
  113.                        
  114.                     send.setOnClickListener(new View.OnClickListener() {   
  115.   
  116.                             @Override  
  117.                             public void onClick(View v) {   
  118.                                    
  119.                                 EditText tel = (EditText) findViewById(R.id.telNum);        //得到需要发送的号码对象   
  120.                                 String telNum = tel.getText().toString();               //得到需要发送的电话号码   
  121.                                 String contextValue = content.getText().toString();     //得到需要发送的短信内容   
  122.                                    
  123.                                 if(telNum == null || "".equals(telNum.trim())) {            //如果号码为空,直接返回。   
  124.                                     return;   
  125.                                 }   
  126.                                        
  127.                                 SmsManager smsManager = SmsManager.getDefault();            //得到短信发送管理对象   
  128.                                 List<String> msgList = smsManager.divideMessage(contextValue);    //由于每条短信中文字数最多70字,所以需要将发送内容进行分段处理(x<=70字 返回的List的size是1)   
  129.                                 for(String msg : msgList) {   
  130.                                     smsManager.sendTextMessage(telNum, null, msg, nullnull);  //根据每段信息,分别发送。   第一个参数:发送号码, 第二个参数:短信中心号码  第三个参数:发送信息, 第四个参数:是否发送成功, 第五个参数: 对方是否接收成功。   
  131.                                 }   
  132.                                    
  133.                                 Toast toast = Toast.makeText(MsgSenderActivity.this, R.string.success, Toast.LENGTH_LONG);      //创建土司提示对象, 第一个参数,需要显示的Activity对象, 第二个参数: 需要显示的内容, 第三个参数:需要显示的时间长度   
  134.                                 toast.setMargin(RESULT_CANCELED, 0.345f);                                   //设置提示对象显示的位置。 第一个参数: 显示的 横向, 第二个参数 显示的纵向   
  135.                                 toast.show();                                                       //显示此提示   
  136.                             }   
  137.                            
  138.                     });   
  139.                        
  140.                     cancel.setOnClickListener(new View.OnClickListener() {   
  141.                             @Override  
  142.                             public void onClick(View v) {   
  143.                                 content.setText("");            //将短信的文本制空。   
  144.                             }   
  145.                     });   
  146.                    
  147.                    
  148.                    
  149.                    
  150.                 }   
  151.             }  
posted @ 2013-06-07 16:27  花叶两不见  阅读(221)  评论(0编辑  收藏  举报