android打电话,发短信

来源:互联网 发布:淘宝上卖水果 怎么申请 编辑:程序博客网 时间:2024/05/07 23:57

                  此处使用两种方法实现打电话及发短信的功能。

         首先是打电话:

                 第一种方法是调用Android自带的功能实现:

                                 Intent  intent  =  new Intent(Intent.Action.CALL_BUTTON);

                                 startActivity(intent);

                如此即可打开拔号键,然后通过拔号进行打电话。

                第二种方法是传入电话号码直接拔打电话:

                                Intent intent  =  new Intent();

                                intent.setAction(Intent.ACTION_CALL);

                                intent.setData(Uri.parse("tel:" + phoneNum));

                                startActivity(intent);

                此处只要传入参数phoneNum即可。

             同时在Manifest.xml中声明权限:

                <uses-permission android:name="android.permission.CALL_PHONE"/>  //允许用户传入号码直接拔号,不需要通过用户界面确认
                <uses-permission android:name="android.permission.CALL_PRIVILEGED"/>   //允许程序将电话号码传给拔号程序,需要用户确认才会拔出电话

  

       发送短信

       同样第一种方法是直接调用系统的发短信界面:

                            Intent intent = new Intent();
                           ComponentName componentName = new ComponentName("com.android.mms","com.android.mms.ui.ConversationList");
                           intent.setComponent(componentName);
                           intent.setAction("android.intent.action.VIEW");
                          startActivity(intent);

    第二种方法是传入参数直接发送短信,支持群发:

                         

private void sendMsg(String[] phoneNum, String msg) {  for (int i = 0; i < phoneNum.length; i++) {   if (isPhoneNumberValid(phoneNum[i]) == true) {    if ((phoneNum[i] == null || "".equals(phoneNum[i]))      || (msg == null || "".equals(msg))) {     Toast.makeText(this, "发送失败!", Toast.LENGTH_LONG).show(); // 显示一个Toast提示    } else {     // ---sends an SMS message to another device---     // SmsManager sms = SmsManager.getDefault();     String SENT_SMS_ACTION = "SENT_SMS_ACTION";     String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";     // create the sentIntent parameter     Intent sentIntent = new Intent(SENT_SMS_ACTION);     PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,       sentIntent, 0);     // create the deilverIntent parameter     Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);     PendingIntent deliverPI = PendingIntent.getBroadcast(this,       0, deliverIntent, 0);// register the Broadcast Receivers     registerReceiver(new BroadcastReceiver() {      @Override      public void onReceive(Context _context, Intent _intent) {       switch (getResultCode()) {       case Activity.RESULT_OK:        Toast.makeText(getBaseContext(), "短信发送成功",          Toast.LENGTH_SHORT).show();        break;       case SmsManager.RESULT_ERROR_GENERIC_FAILURE:        Toast.makeText(getBaseContext(), "短信发送失败",          Toast.LENGTH_SHORT).show();        break;       case SmsManager.RESULT_ERROR_RADIO_OFF:        // Toast.makeText(getBaseContext(),        // "SMS radio off failure actions",        // Toast.LENGTH_SHORT).show();        break;       case SmsManager.RESULT_ERROR_NULL_PDU:        // Toast.makeText(getBaseContext(),        // "SMS null PDU failure actions",        // Toast.LENGTH_SHORT).show();break;       }      }     }, new IntentFilter(SENT_SMS_ACTION));     registerReceiver(new BroadcastReceiver() {      @Override      public void onReceive(Context _context, Intent _intent) {       // Toast.makeText(getBaseContext(),       // "SMS delivered actions", Toast.LENGTH_SHORT)       // .show();      }     }, new IntentFilter(DELIVERED_SMS_ACTION));     SmsManager manage = SmsManager.getDefault(); // 取得默认的SmsManager用于短信的发送     List<String> all = manage.divideMessage(msg); // 短信的内容是有限的,要根据短信长度截取。逐条发送     Iterator<String> it = all.iterator();     while (it.hasNext()) {      manage.sendTextMessage(phoneNum[i], null, it.next(),        sentPI, deliverPI); // 逐条发送短息     }    }   }  } }


                           同样要加上权限:<uses-permission android:name="android.permission.SEND_SMS"/>

                           如此大致算是完成了。