Android實(shí)現(xiàn)短信發(fā)送功能
本文實(shí)例實(shí)現(xiàn)了兩個(gè)模擬器之間短信的發(fā)送功能,分享給大家供大家參考,具體實(shí)現(xiàn)內(nèi)容如下
1.編輯String.xml文件內(nèi)容為:
<?xml version=”1.0″ encoding=”utf-8″?> <resources> <string name=”app_name”>SendMesage</string> <string name=”action_settings”>Settings</string> <string name=”hello_world”>Hello world!</string> <string name=”hello”>請輸入手機(jī)號碼</string> <string name=”content”>Please input your message</string> <string name=”send”>Send</string> </resources>
2.編輯main.xml的內(nèi)容為:
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:paddingBottom=”@dimen/activity_vertical_margin” android:paddingLeft=”@dimen/activity_horizontal_margin” android:paddingRight=”@dimen/activity_horizontal_margin” android:paddingTop=”@dimen/activity_vertical_margin” tools:context=”.SendMessage” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello”/> <EditText android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:id=”@+id/mobile”/> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/content”/> <EditText android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:minLines=”3″ android:id=”@+id/message”/> <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”@string/send” android:id=”@+id/send”/> </LinearLayout>
3. SendMessage.java
import java.util.List; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class SendMessage extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); Button button = (Button)this.findViewById(R.id.send); button.setOnClickListener(new OnClickListener() { // 注冊按鈕單擊事件 @Override public void onClick(View arg0) { EditText mobileText = (EditText)findViewById(R.id.mobile);//獲取電話號碼 String mobile = mobileText.getText().toString(); EditText messageText = (EditText)findViewById(R.id.message);//獲取短信內(nèi)容 String message = messageText.getText().toString(); // 移動(dòng)運(yùn)營商允許每次發(fā)送的字節(jié)數(shù)據(jù)有限,可以使用Android提供的短信工具。 if(message != null) { SmsManager sms = SmsManager.getDefault(); List texts = sms.divideMessage(message); for(String text : texts) { sms.sendTextMessage(mobile, null, text, null, null); } } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.send_message, menu); return true; } }
4. 編輯AndroidManifest.xml內(nèi)容,添加一條語句即可
<!– 注冊發(fā)送短信的權(quán)限 –> <uses-permission android:name=”android.permission.SEND_SMS” />
如果沒有注冊這個(gè)短信發(fā)送的權(quán)限,將使用不了系統(tǒng)的短信發(fā)送功能。在其他應(yīng)該開發(fā)中,有使用到系統(tǒng)功能的時(shí)候,必須在這個(gè)文件中注冊。
5. 啟動(dòng)兩個(gè)模擬器
- 第一個(gè)模擬器,通過eclipse啟動(dòng),運(yùn)行該應(yīng)用即可啟動(dòng)一個(gè)android模擬器。ID通常為5554.
- 第二個(gè)模擬器,通過DOS命令啟動(dòng)。CD到sdk的tools目錄下,用命令:emulator -avd “AVD名稱”
6. 使用模擬器的ID作為電話號碼發(fā)送短信
效果圖:
希望本文所述對大家學(xué)習(xí)Android軟件編程有所幫助。
- 簡單實(shí)現(xiàn)android短信發(fā)送器
- Android實(shí)戰(zhàn)教程第四篇之簡單實(shí)現(xiàn)短信發(fā)送器
- Android基礎(chǔ)開發(fā)小案例之短信發(fā)送器
- Android開發(fā)之電話撥號器和短信發(fā)送器實(shí)現(xiàn)方法
- Android短信發(fā)送器實(shí)現(xiàn)方法
- Android Mms之:短信發(fā)送流程(圖文詳解)
- 利用smsmanager實(shí)現(xiàn)后臺(tái)發(fā)送短信示例
- Android發(fā)送短信功能代碼
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能
相關(guān)文章
Android中RecycleView與ViewPager沖突的解決方法及原理
這篇文章主要給大家介紹了關(guān)于Android中RecycleView與ViewPager沖突的解決方法及原理的相關(guān)資料,以及ViewPager嵌套R(shí)ecycleView卡頓問題的處理方法,文中通過示例代碼介紹的非常狎昵,需要的朋友可以參考下2018-07-07Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書中都沒有對PreferenceActivity做介紹,而我正好又在項(xiàng)目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11Android自定義View繪制貝塞爾曲線中小紅點(diǎn)的方法
貝塞爾曲線的本質(zhì)是通過數(shù)學(xué)計(jì)算的公式來繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學(xué)公式和驗(yàn)證,那些偉大的數(shù)學(xué)家已經(jīng)證明過了,所以就只講講Android開發(fā)中的運(yùn)用吧2023-02-02Android網(wǎng)絡(luò)連接判斷與相關(guān)處理
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)連接判斷操作,幫助大家判斷WIFI網(wǎng)絡(luò)是否可用,判斷MOBILE網(wǎng)絡(luò)是否可用,感興趣的小伙伴們可以參考一下2016-08-08