Android_簡易的簡訊發送器

來源:互聯網
上載者:User

標籤:擷取   def   utf-8   需要   oid   ack   void   允許   edittext   

這個隨筆將介紹如何完成一個簡單的第三方的簡訊發送器(不開啟簡訊介面,調用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     <EditText15         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     <EditText26         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     <Button34         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     @Override12     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         //直接使用傳送簡訊的api25         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         }

 

 然後到這裡就結束了。以上

Android_簡易的簡訊發送器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.