Step1 先了解這一行之參數
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
1.destinationAddress發送出去那兒的號碼,收訊息者號碼。
2.scAddress訊息服務中心號碼,填null即可。
3. text簡訊內容
4. sentIntent訊息發送結果廣播
5.deliveryIntent訊息到達廣播
Step2 在AndroidManifest.xml加入權限
<uses-permission android:name="android.permission.SEND_SMS"/>
小試身手小範例
JAVA檔
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class SMStest extends Activity {
/** Called when the activity is first created. */
private EditText editTextReceiver;
private EditText editTextContent;
private Button buttonSend;
private Button buttonCancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
editTextReceiver = (EditText)findViewById(R.id.EditTextReceiver);
editTextContent = (EditText)findViewById(R.id.EditTextContent);
buttonSend = (Button)findViewById(R.id.ButtonSend);
buttonCancel = (Button)findViewById(R.id.ButtonCancel);
buttonSend.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SmsManager smsManager = SmsManager.getDefault();
try{
smsManager.sendTextMessage(editTextReceiver.getText().toString(),
null, editTextContent.getText().toString(),
PendingIntent.getBroadcast(getApplicationContext(), 0,new Intent(), 0),
null);
}catch(Exception e){
e.printStackTrace();
};
}
});
}
}
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="簡訊發送系統"
android:textSize="25sp"
android:layout_margin="10sp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收件人 "
android:textSize="20sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditTextReceiver"
/>
</LinearLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="100sp"
android:id="@+id/EditTextContent"
android:layout_margin="10sp"
android:hint="傳送內容..."
android:gravity="top|left"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20sp"
>
<Button
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text=" 發 送 "
android:textSize="20sp"
android:id="@+id/ButtonSend"
/>
<Button
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text=" 取 消 "
android:layout_marginLeft="10sp"
android:textSize="20sp"
android:id="@+id/ButtonCancel"
/>
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMStest"
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>
</manifest>
測試的時候將兩個模擬器開啟,可參考這篇 http://style77125tech.pixnet.net/blog/post/149637274
左邊模擬器為5554號,右邊模擬器維5556號,以下為5554發訊息給5556
留言列表