2011年12月9日 星期五

利用Intent傳送簡訊(以模擬器測試)

利用Intent傳送簡訊(以模擬器測試)

當一個SMS訊息被接收時,一個新的Intent由android.provider.Telepony.SMS_RECEIVED動作觸發。現在我們就開始建置一個SMS接收程序:
1)、跟SMS發送程序類似,要在清單文件AndroidManifest.xml中指定權限允許接收SMS:
<uses-permission android:name="android.permission.RECEIVER_SMS"/>

XML

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/widget1" >

    <TextView android:id="@+id/widget2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="寫訊息:"
        android:textSize="16sp"
        android:layout_x="0px"
        android:layout_y="12px"
      >
      </TextView>  
    <EditText android:id="@+id/phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="18sp"
        android:layout_x="60px"
        android:layout_y="2px"
        />
    <EditText android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="223px"
        android:text=""
        android:textSize="18sp"
        android:layout_x="0px"
        android:layout_y="52px"
        />
 
    <Button android:id="@+id/submit"
        android:layout_width="162px"
        android:layout_height="wrap_content"
        android:text="送出"
        android:layout_x="80px"
        android:layout_y="302px"
        />
</AbsoluteLayout>

JAVA檔

package com.demo.android.Message;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MessageActivity extends Activity {
private Button submit;
private EditText phone;
private EditText message;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //利用  findViewById建構子建構元件
        submit = (Button) findViewById(R.id.submit);
        phone = (EditText) findViewById(R.id.phone);
        message = (EditText) findViewById(R.id.message);

        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            //傳入的訊息轉成文字
                String pphone = phone.getText().toString();
                String pmessage = message.getText().toString();
             
                if (phoneNo.length() > 0 && pmessage.length() > 0) {
                //利用Intent傳送資料,創造解析特定編碼URI
                     Intent sms=new Intent(Intent.ACTION_SENDTO,
                    Uri.parse("sms:"+phone.getText().toString()));
                     sms.putExtra("putsms",message.getText().toString());
                     MessageActivity.this.startActivity(sms);
                } else
                    Toast.makeText(getBaseContext(),
                            ".......................................................",
                            Toast.LENGTH_SHORT).show();
             
                phone.setText(" ");
                message.setText(" ");
            }
        });
    }
}




利用Intent將我們的訊息傳遞给內建SMS客戶端發送我們SMS。如果要做到這樣效果,就必須要用到startActivity("指定一個Intent")方法,且指定Intent的動作為Intent.ACTION_SENDTO,用sms:指定目標手機號,用sms_body指定信息內容。


沒有留言:

張貼留言