類比android簡訊發送器的實現
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="請輸入簡訊內容" />
<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="發送"
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系統中預設的簡訊管理器
SmsManager manager=SmsManager.getDefault();
//如果簡訊內容過長時,則對簡訊內容進行拆分
ArrayList<String> texts=manager.divideMessage(content);
for(String text:texts){
//第一個參數:對方手機號碼
//第二個參數:簡訊中心號碼,一般設定為空白
//第三個參數:簡訊內容
//第四個參數:sentIntent判斷簡訊是否發送成功,如果你沒有SIM卡,或者網路中斷,則可以通過這個intent來判斷。
//注意強調的是“發送”的動作是否成功。那麼至於對於對方是否收到,另當別論
//第五個參數:當簡訊發送到收件者時,會收到這個deliveryIntent。即強調了“發送”後的結果
//就是說是在"簡訊發送成功"和"對方收到此簡訊"才會啟用sentIntent和deliveryIntent這兩個Intent。這也相當於是順延強制了Intent
manager.sendTextMessage(mobile, null, text, null, null);
}
//Toast.makeText(getApplicationContext(), "發送成功", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_LONG).show();
}
});
}
}
運行結果