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="請輸入手機(jī)號" 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)中默認(rèn)的短信管理器
SmsManager manager=SmsManager.getDefault();
//如果短信內(nèi)容過長時,則對短信內(nèi)容進(jìn)行拆分
ArrayList<String> texts=manager.divideMessage(content);
for(String text:texts){
//第一個參數(shù):對方手機(jī)號碼
//第二個參數(shù):短信中心號碼,一般設(shè)置為空
//第三個參數(shù):短信內(nèi)容
//第四個參數(shù):sentIntent判斷短信是否發(fā)送成功,如果你沒有SIM卡,或者網(wǎng)絡(luò)中斷,則可以通過這個intent來判斷。
//注意強(qiáng)調(diào)的是“發(fā)送”的動作是否成功。那么至于對于對方是否收到,另當(dāng)別論
//第五個參數(shù):當(dāng)短信發(fā)送到收件人時,會收到這個deliveryIntent。即強(qiáng)調(diào)了“發(fā)送”后的結(jié)果
//就是說是在"短信發(fā)送成功"和"對方收到此短信"才會激活sentIntent和deliveryIntent這兩個Intent。這也相當(dāng)于是延遲執(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自定義ViewGroup(側(cè)滑菜單)詳解及簡單實例
這篇文章主要介紹了Android自定義ViewGroup(側(cè)滑菜單)詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android獲取熱點主機(jī)ip和連接熱點手機(jī)ip的代碼
這篇文章主要介紹了Android獲取熱點主機(jī)ip和連接熱點手機(jī)ip的相關(guān)資料,需要的朋友可以參考下2018-01-01
Android學(xué)習(xí)之使用SharedPreferences存儲應(yīng)用程序數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)之使用SharedPreferences保存應(yīng)用程序數(shù)據(jù)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
Android依據(jù)名字通過反射獲取在drawable中的圖片
依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android Broadcast原理分析之registerReceiver詳解
這篇文章主要介紹了Android Broadcast原理分析之registerReceiver詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android ExpandableListView單選以及多選實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android ExpandableListView單選以及多選的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Jetpack?Compose?的新型架構(gòu)?MVI使用詳解
這篇文章主要介紹了Jetpack?Compose?的新型架構(gòu)?MVI使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Win10下Android App安裝配置開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

