標籤:
這一節學習了簡訊發送功能的app開發,效果
前台,activity_main.xml代碼:
<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:orientation="vertical" 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="com.swust.msg.MainActivity" > <!-- 輸入為phone類型:0~9和#* --> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/tishiPhone" android:inputType="phone" /> <!-- 輸入限制為五行,輸入框中的內容頂部對齊 --> <EditText android:id="@+id/et_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:hint="@string/tishiMsg" android:lines="5" /> <Button android:id="@+id/btnSend" android:layout_width="119dp" android:layout_height="64dp" android:text="發送" android:onClick="sendMsg"/></LinearLayout>
後台,MainActivity.java代碼:
package com.swust.msg;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMsg(View v){ //擷取到使用者輸入的號碼和內容 EditText et_phone = (EditText)findViewById(R.id.et_phone); EditText et_msg = (EditText)findViewById(R.id.et_msg); String phone=et_phone.getText().toString(); String msg=et_msg.getText().toString(); //傳送簡訊 //1.擷取短息管理器 SmsManager sm = SmsManager.getDefault(); //2.傳送簡訊 /*第二個形參為短息服務中心,為空白即可 *第三個形參為發送廣播,為空白即可 *第4個形參為接受廣播,為空白即可*/ //sm.sendTextMessage(phone, null, msg, null, null); //2.切割簡訊,把長簡訊分成若干個小簡訊 ArrayList<String> smss= sm.divideMessage(msg); //3.for迴圈把集合中所有簡訊全部發出去 for(String string:smss){ sm.sendTextMessage(phone, null, string, null, null); } }}
當然需要加許可權,在資訊清單檔Permission中有如下許可權:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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>
第二版:
關於傳送簡訊的核心語句解釋參照這裡:
http://www.cnblogs.com/nktblog/articles/2607086.html
package com.swust.msg;import java.util.ArrayList;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsManager;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMsg(View v){ //擷取到使用者輸入的號碼和內容 EditText et_phone = (EditText)findViewById(R.id.et_phone); EditText et_msg = (EditText)findViewById(R.id.et_msg); String phone=et_phone.getText().toString(); String msg=et_msg.getText().toString(); //傳送簡訊 //1.擷取短息管理器 SmsManager sm = SmsManager.getDefault(); //2.傳送簡訊 /*第二個形參為短息服務中心,為空白即可 *第三個形參為發送廣播,為空白即可 *第4個形參為接受廣播,為空白即可*/ //sm.sendTextMessage(phone, null, msg, null, null); //2.切割簡訊,把長簡訊分成若干個小簡訊 ArrayList<String> smss= sm.divideMessage(msg); //3.for迴圈把集合中所有簡訊全部發出去 for(String string:smss){ sm.sendTextMessage(phone, null, string, null, null); } /*首先判讀電話長度為11或者6(測試我們學校)並且內容不為空白,將資訊發送出去;發送成功或失敗後返回提示 缺點是,沒有使用切割簡訊迴圈發功能,直接將資訊(msg)發送了*/ if( (phone.trim().length()==11||phone.trim().length()==6) &&msg.trim().length()!=0){ PendingIntent pintent=PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0); sm.sendTextMessage(phone, null, msg, pintent, null); Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "發送失敗", Toast.LENGTH_SHORT).show(); } }}
簡訊發送器(1.0版)