Android短信發(fā)送器實現(xiàn)方法
本文實例講述了Android短信發(fā)送器實現(xiàn)方法。分享給大家供大家參考。具體如下:
這里模擬android短信發(fā)送器的實現(xiàn)
AndroidManifest.xml清單文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.sms" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.SEND_SMS"/> </manifest>
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="115dip" android:layout_height="wrap_content" android:text="請輸入手機號" android:id="@+id/mobilelabel" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/mobilelabel" android:text="5556" android:id="@+id/mobile" /> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請輸入短信內(nèi)容" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:text="I am a teacher!" android:id="@+id/content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送" android:id="@+id/button" /> </LinearLayout>
MainActivity類:
package com.ljq.sms; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText mobileText=null; private EditText contentText=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mobileText=(EditText)findViewById(R.id.mobile); contentText=(EditText)findViewById(R.id.content); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { String mobile=mobileText.getText().toString(); String content=contentText.getText().toString(); //取得android系統(tǒng)中默認的短信管理器 SmsManager manager=SmsManager.getDefault(); //如果短信內(nèi)容過長時,則對短信內(nèi)容進行拆分 ArrayList<String> texts=manager.divideMessage(content); for(String text:texts){ //第一個參數(shù):對方手機號碼 //第二個參數(shù):短信中心號碼,一般設(shè)置為空 //第三個參數(shù):短信內(nèi)容 //第四個參數(shù):sentIntent判斷短信是否發(fā)送成功,如果你沒有SIM卡,或者網(wǎng)絡(luò)中斷,則可以通過這個intent來判斷。 //注意強調(diào)的是“發(fā)送”的動作是否成功。那么至于對于對方是否收到,另當別論 //第五個參數(shù):當短信發(fā)送到收件人時,會收到這個deliveryIntent。即強調(diào)了“發(fā)送”后的結(jié)果 //就是說是在"短信發(fā)送成功"和"對方收到此短信"才會激活sentIntent和deliveryIntent這兩個Intent。這也相當于是延遲執(zhí)行了Intent manager.sendTextMessage(mobile, null, text, null, null); } //Toast.makeText(getApplicationContext(), "發(fā)送成功", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "發(fā)送成功", Toast.LENGTH_LONG).show(); } }); } }
運行結(jié)果:
希望本文所述對大家的Android程序設(shè)計有所幫助。
- 簡單實現(xiàn)android短信發(fā)送器
- Android實戰(zhàn)教程第四篇之簡單實現(xiàn)短信發(fā)送器
- Android基礎(chǔ)開發(fā)小案例之短信發(fā)送器
- Android開發(fā)之電話撥號器和短信發(fā)送器實現(xiàn)方法
- Android實現(xiàn)短信發(fā)送功能
- Android Mms之:短信發(fā)送流程(圖文詳解)
- 利用smsmanager實現(xiàn)后臺發(fā)送短信示例
- Android發(fā)送短信功能代碼
- Android實現(xiàn)發(fā)送短信功能實例詳解
- Android使用SmsManager實現(xiàn)短信發(fā)送功能
相關(guān)文章
Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時間
本文通過實例代碼給大家介紹了Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時間,需要的朋友可以參考下2017-07-07Android ViewPager實現(xiàn)滑動指示條功能
這篇文章主要介紹了Android-ViewPager實現(xiàn)滑動指示條功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10基于Android studio3.6的JNI教程之ncnn之語義分割ENet
這篇文章主要介紹了基于Android studio3.6的JNI教程之ncnn之語義分割ENet的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-03-03Flutter 首頁必用組件NestedScrollView的示例詳解
今天介紹的組件是NestedScrollView,大部分的App首頁都會用到這個組件。對Flutter 首頁必用組件NestedScrollView的相關(guān)知識感興趣的一起看看吧2020-05-05Android 關(guān)于“NetworkOnMainThreadException”問題的原因分析及解決辦法
這篇文章主要介紹了Android 關(guān)于“NetworkOnMainThreadException”的相關(guān)知識,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學(xué)習吧2016-02-02Android 應(yīng)用的歡迎界面實現(xiàn)代碼
本文主要介紹Android 應(yīng)用歡迎界面的開發(fā),這里提供實現(xiàn)方法和實現(xiàn)代碼以供大家參考,有需要的朋友可以參考下2016-07-07