天天看點

Android發短信功能

發短信

    方式一:直接發送短信

    1、添加發送短信的權限在manifast檔案中。

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

    2、擷取android.telephony.SmsManager對象(PS:android.telephony.gsm.SmsManager已經廢棄)。

        SmsManager smsManager = SmsManager.getDefault();

    3、聲明一個短信内容的常量。

       String content = "Hello World!";

    4、将短信内容分塊,發送一條短信最多能夠發送70個中文字元,超過這個值系統會将短信内容分為多塊進行發送。

        ArrayList<String> list = smsManager.divideMessage(content);

    5、分條進行發送。

       for (int i = 0; i < list.size(); i++) {

                smsManager.sendTextMessage("10086", null, list.get(i), null, null);

            }

    方式二:調用系統的發送短信的界面,需要輸入号碼

     1、建立意圖

    Intent intentFinalMessage = new Intent(Intent.ACTION_VIEW);

    2、設定類型

    intentFinalMessage.setType("vnd.android-dir/mms-sms");

      3、打開系統短信界面

     startActivity(intentFinalMessage); 

     方式三:調用系統的發送短信的界面,不需要輸入号碼

     1、建立Uri,設定行為和号碼

     Uri uri2 = Uri.parse("smsto:"+10086);

     2、建立意圖。     

     Intent intentMessage = new Intent(Intent.ACTION_VIEW,uri2);

    3、打開系統短信界面,号碼已經填寫,隻需填寫要發送   

        startActivity(intentMessage);

三、

利用類 SmsManager 發送資訊, smsManager 為 SmsManager 一個預設的執行個體. SmsManager smsManager = SmsManager.getDefault(); 

  smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent) 

sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

參數說明:

      1、destinationAddress:給這個号碼發送短信。

      2、scAddress:使用這個号碼發送短信,為null時表示使用本機發送。

      3、text:短信内容。

      4、sentIntent:發送短信成功或失敗之後發送廣播。

      5、deliveryIntent:對方接受到短信之後發送廣播。

四、發送廣播的短信發送

      1、注冊廣播

            注冊自己發送短信的廣播:

            SEND_SMS_ACTION = "3";

        registerReceiver(new BroadcastReceiver(){

            @Override

            public void onReceive(Context context, Intent intent) {

                //根據結果碼判斷是否發送成功

                if(Activity.RESULT_OK == getResultCode()){

                    Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_SHORT).show();

                }else{

                    Toast.makeText(MainActivity.this, "發送失敗", Toast.LENGTH_SHORT).show();

                }

            }

        }, new IntentFilter(SEND_SMS_ACTION));

         注冊對方接受到短信的廣播:

         BACK_SMS_ACTION = "4";

        registerReceiver(new BroadcastReceiver(){

            @Override

            public void onReceive(Context context, Intent intent) {

                //隻要接收到這個廣播,表示接收短信成功

                Toast.makeText(MainActivity.this, "對方接收到短信", Toast.LENGTH_SHORT).show();

            }

        }, new IntentFilter(BACK_SMS_ACTION));

        2、發送 短信,監聽發送情況、監聽對方接受情況,如果短信發送成功或失敗

            PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0)會發送廣播,

            如果對方接受到短信

           PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(BACK_SMS_ACTION), 0));會發送廣播

          for (int i = 0; i < list.size(); i++) {

                smsManager.sendTextMessage("10086", null, list.get(i),

                        PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0),

                        PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(BACK_SMS_ACTION), 0));

           }

五、使用smsManager發送其它格式的短信

     1、

sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent

  deliveryIntent)

  其它參數一樣,第三個參數

short destinationPort,給這個号碼的這個端口号發送這條短信,短信内容為位元組數組格式。

 2、

sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,ArrayList<PendingIntent> sentIntents,   ArrayList<PendingIntent>  deliveryIntents)

  給這個号碼發送多條短信。