1. 若需使用發送SMS的服務,需在AndroidManifest.xml允許存取android.permission.SEND_SMS這個權限。
2.SendSMSExample.java
01.
package
tw.nicky.SendSMSExample;
02.
03.
import
android.app.Activity;
04.
import
android.app.PendingIntent;
05.
import
android.content.Intent;
06.
import
android.os.Bundle;
07.
import
android.telephony.SmsManager;
08.
import
android.view.View;
09.
import
android.widget.Button;
10.
import
android.widget.EditText;
11.
12.
public
class
SendSMSExample
extends
Activity {
13.
private
EditText phoneNumEditText;
14.
private
EditText msgEditText;
15.
private
Button sendButton;
16.
@Override
17.
public
void
onCreate(Bundle savedInstanceState) {
18.
super
.onCreate(savedInstanceState);
19.
setContentView(R.layout.main);
20.
//電話號碼
21.
phoneNumEditText = (EditText)findViewById(R.id.phoneNumEditText);
22.
//訊息
23.
msgEditText = (EditText)findViewById(R.id.msgEditText);
24.
sendButton = (Button)findViewById(R.id.sendButton);
25.
26.
//按下發送按鈕
27.
sendButton.setOnClickListener(
new
View.OnClickListener() {
28.
29.
@Override
30.
public
void
onClick(View v) {
31.
//取得預設的SmsManager
32.
SmsManager smsManager = SmsManager.getDefault();
33.
PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSMSExample.
this
,
0
,
new
Intent(),
0
);
34.
//傳送SMS
35.
smsManager.sendTextMessage(phoneNumEditText.getText().toString(),
null
, msgEditText.getText().toString(), pendingIntent,
null
);
36.
}
37.
});
38.
}
39.
}
3. main.xml(Layout)
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03.
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
04.
android:layout_height
=
"fill_parent"
>
05.
<
TextView
06.
android:layout_width
=
"fill_parent"
07.
android:layout_height
=
"wrap_content"
08.
android:text
=
"@string/hello"
/>
09.
<
EditText
10.
android:id
=
"@+id/phoneNumEditText"
11.
android:layout_width
=
"wrap_content"
12.
android:layout_height
=
"wrap_content"
></
EditText
>
13.
<
EditText
14.
android:id
=
"@+id/msgEditText"
15.
android:layout_width
=
"wrap_content"
16.
android:layout_height
=
"wrap_content"
></
EditText
>
17.
<
Button
18.
android:id
=
"@+id/sendButton"
19.
android:text
=
"發送"
20.
android:layout_width
=
"wrap_content"
21.
android:layout_height
=
"wrap_content"
></
Button
>
22.
</
LinearLayout
>
4. AndroidManifest.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03.
package
=
"tw.nicky.SendSMSExample"
04.
android:versionCode
=
"1"
05.
android:versionName
=
"1.0"
>
06.
<
application
android:icon
=
"@drawable/icon"
android:label
=
"@string/app_name"
>
07.
<
activity
android:name
=
".SendSMSExample"
08.
android:label
=
"@string/app_name"
>
09.
<
intent-filter
>
10.
<
action
android:name
=
"android.intent.action.MAIN"
/>
11.
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
12.
</
intent-filter
>
13.
</
activity
>
14.
</
application
>
15.
<
uses-permission
android:name
=
"android.permission.SEND_SMS"
/>
16.
</
manifest
>
全站熱搜
留言列表