欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實戰(zhàn)教程第四篇之簡單實現(xiàn)短信發(fā)送器

 更新時間:2016年11月10日 11:54:11   作者:楊道龍  
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)教程第四篇之簡單實現(xiàn)短信發(fā)送器,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android發(fā)短信功能的實現(xiàn)方法,供大家參考,具體內(nèi)容如下

首先配置一個布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_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=".MainActivity" 
 android:orientation="vertical" 
 > 
 
 <EditText 
 android:id="@+id/et_phone" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:inputType="phone" 
 android:hint="請輸入對方號碼" 
 /> 
 <EditText 
 android:id="@+id/et_content" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:lines="5" 
 android:hint="請輸入短信內(nèi)容" 
 android:gravity="top" 
 /> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="發(fā)送" 
 android:onClick="send" 
 /> 
 
</LinearLayout>

 然后在activity中把發(fā)短信的代碼寫出來:

package com.ydl.smssender; 
 
import java.util.ArrayList; 
 
//省略導(dǎo)包 
 
public class MainActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 } 
 
 
 public void send(View v){ 
 //拿到用戶輸入的號碼和內(nèi)容 
 EditText et_phone = (EditText) findViewById(R.id.et_phone); 
 EditText et_content = (EditText) findViewById(R.id.et_content); 
  
 String phone = et_phone.getText().toString(); 
 String content = et_content.getText().toString(); 
  
 //1.獲取短信管理器 
 SmsManager sm = SmsManager.getDefault(); 
  
 //2.切割短信,把長短信分成若干個小短信 
 ArrayList<String> smss = sm.divideMessage(content);//an ArrayList of strings that, in order, comprise the original message 
  
 //3.for循環(huán)把集合中所有短信全部發(fā)出去 
 for (String string : smss) { 
  
  sm.sendTextMessage(phone, null, string, null, null);//Send a text based SMS. 
 } 
 } 
 
} 

發(fā)短信是需要系統(tǒng)權(quán)限的:

復(fù)制代碼 代碼如下:
<uses-permission android:name="android.permission.SEND_SMS"/>
 

效果:

開了兩個模擬器,實現(xiàn)了發(fā)短信功能。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論