這個隨筆將介紹如何完成一個簡單的第三方的短信發送器(不打開短信界面,調用android的api完成功能)
1.首先,我們來做布局
由於我這里寫的是一個簡易的,,短信發送,所以只是一個LinearLayout下放了兩個EditText用來存號碼和內容,還有一個Button發送按鈕,如果想要更華麗的布局,有興趣的大家可以再去添加奧。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.example.sendmessage.MainActivity"> 9 10 <!-- 11 1.使用hint可以顯示提示占位信息,輸入時自動消失 12 2.inputType是輸入數據的類型,這里需要號碼所以允許輸入數字 13 --> 14 <EditText 15 android:id="@+id/et_phone" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:hint="請輸入號碼" 19 android:inputType="number" /> 20 21 <!-- 22 1. 這里建議使用lines去控制輸入的內容多少,如果把高度寫死,不同手機不同大小的字體 23 可以輸入的漢字的總量就會變化,而使用lines無論多大的字都是可以輸入固定行數的字 24 --> 25 <EditText 26 android:id="@+id/et_content" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:hint="請輸入短信內容" 30 android:lines="8" 31 android:gravity="top"/> 32 33 <Button 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:onClick="send" 37 android:text="發送" /> 38 39 </LinearLayout>
鹽后,就是業務處理了,重點來了,可以拿板凳了啊,我們可以直接調用安卓發送短信的api,只需要兩行代碼,獲取對象發送短信即可
1 package com.example.sendmessage; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.telephony.SmsManager; 6 import android.view.View; 7 import android.widget.EditText; 8 9 public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 } 16 17 public void send(View v){ 18 //獲取用戶輸入的號碼以及短信內容 19 EditText et_phone = (EditText)findViewById(R.id.et_phone); 20 EditText et_content = (EditText)findViewById(R.id.et_content); 21 String phone = et_phone.getText().toString(); 22 String content = et_content.getText().toString(); 23 24 //直接使用發送短信的api 25 SmsManager sm = SmsManager.getDefault(); 26 27 /** 28 * arg0:目標號碼 29 * arg1:短信中心號碼,null即可,寫死了到換了地方不就沒辦法打電話了 30 * arg2:短信正文 31 */ 32 sm.sendTextMessage(phone, null, content, null, null); 33 } 34 35 36 }
(sendTextMessage第二個參數,大家如果有興趣,可以打開自己手機的撥號界面,按下‘*#*#6436#*#*’,在選擇‘手機信息’->向下拉可以看到SMSC這個地方就是空的,所有程序中我們傳了一個null)
然后我們懷着激動的心情點了運行程序,,輸入了電話和密碼,然后,,,,神奇的一幕發生了
停止運行,為什么呢????別怪我挖坑,我們先看一看報錯信息
這就說的很清楚了嘛,沒有發送短信的權限,那我們去AndroidManifest.xml中去添加這個權限
在<application>之前添加
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
這句話,然后重新運行
終於成功了,事情結束了嗎???還差一點,還有一個事情就是運營商規定一條短信最多只能發送70個字,那問題來了,生活中我們發幾百字的甜言蜜語是怎么發出去的呢?那是因為后台程序已經把短信拆分成多條了
同理,會收取多條的費用,那么運營商肯定不管拆分,他們只負責收錢,拆分就交給我們了,
1 //直接使用發送短信的api 2 SmsManager sm = SmsManager.getDefault(); 3 4 //拆分長短信 5 ArrayList<String> sms = sm.divideMessage(content); 6 for(String string : sms){ 7 8 /** 9 * arg0:目標號碼 10 * arg1:短信中心號碼,null即可,寫死了到換了地方不就沒辦法打電話了 11 * arg2:短信正文 12 */ 13 sm.sendTextMessage(phone, null, string, null, null); 14 }
然后到這里就結束了。以上