2.SendSMSExample.java
package tw.nicky.SendSMSExample; 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.widget.Button; import android.widget.EditText; public class SendSMSExample extends Activity { private EditText phoneNumEditText; private EditText msgEditText; private Button sendButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //電話號碼 phoneNumEditText = (EditText)findViewById(R.id.phoneNumEditText); //訊息 msgEditText = (EditText)findViewById(R.id.msgEditText); sendButton = (Button)findViewById(R.id.sendButton); //按下發送按鈕 sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //取得預設的SmsManager SmsManager smsManager = SmsManager.getDefault(); PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSMSExample.this, 0, new Intent(), 0); //傳送SMS smsManager.sendTextMessage(phoneNumEditText.getText().toString(), null, msgEditText.getText().toString(), pendingIntent, null); } }); } }3. main.xml(Layout)
<?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="@string/hello" /> <EditText android:id="@+id/phoneNumEditText" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> <EditText android:id="@+id/msgEditText" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/sendButton" android:text="發送" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tw.nicky.SendSMSExample" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SendSMSExample" 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-permission android:name="android.permission.SEND_SMS" /> </manifest>
5. 執行畫面
沒有留言:
張貼留言