Android實現發簡訊和彩信功能

首先 activity_ 佈局:

Android實現發簡訊和彩信功能

<RelativeLayout xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="ctivity" >

<TextView

android:id="@+id/phone_lable"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_marginTop="23dp"

android:text="@string/phon_lable" />

<EditText

android:id="@+id/edit_phone"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/phone_lable"

android:layout_centerHorizontal="true"

android:ems="10"

android:inputType="phone" >

<TextView

android:id="@+id/sms_lable"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/edit_phone"

android:layout_below="@+id/edit_phone"

android:layout_marginTop="22dp"

android:text="@string/sms_lable" />

<Button

android:id="@+id/But_sms"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/edit_sms"

android:layout_alignParentBottom="true"

android:layout_marginBottom="14dp"

android:text="@string/But_sms" />

<EditText

android:id="@+id/edit_sms"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_above="@+id/But_sms"

android:layout_alignLeft="@+id/sms_lable"

android:ems="10"

android:inputType="textMultiLine" />

然後在 中新增 傳送簡訊的許可權:

接下來就是SmsActivity 中 主要程式碼 的'實現 其功能 :

public class SmsActivity extends Activity {

// 宣告控制元件物件

private Button butten_sms;

private EditText edit_phone;

private EditText edit_sms;

@Override

protected void onCreate(Bundle savedInstanceState) {

eate(savedInstanceState);

//設定顯示檢視

setContentView(vity_sms);

//獲取按鈕元件

butten_sms=(Button) findViewById(_sms);

//獲取輸入框元件

edit_phone = (EditText) findViewById(_phone);

edit_sms = (EditText) findViewById(_sms);

//註冊按鈕事件

butten_nClickListener(new ickListener() {

@Override

public void onClick(View arg0) {

/**

* 發彩信

*/

//獲取電話

String phone_num = edit_ext()ring();

//建立意圖物件

Intent intent = new Intent(ON_SEND);

xtra(A_STREAM, Uri.parse("file:///sdcard/a.jpg"));

xtra("address", phone_num);

xtra("exit_on_sent", true);

xtra("subject", "subject:I love you");

xtra("sms_body", "content ::XXXX");

intent.setType("image/jpeg");

startActivity(intent);

/*// 獲取簡訊管理器物件

SmsManager smsManager = efault();

// 意圖物件

PendingIntent pendingIntent = roadcast(

, 0, new Intent(), 0);*/

}

});

}

//發簡訊 功能

public void send(){

//獲取號碼

String phone_Num = edit_ext()ring();

//獲取傳送的簡訊

String phone_sms = edit_ext()ring();

//獲取資訊管理器物件

SmsManager smsmanager = efault();

//意圖物件

PendingIntent pendingIntent = roadcast(, 0, new Intent(), 0);

//判斷 資訊長度 大於 70則分兩條傳送。。預設是70

ArrayListcontents = smsmanager

deMessage(phone_sms);

for (String c : contents) {

// 傳送資訊的操作

TextMessage(phone_Num, null, c,

pendingIntent, null);

}

/*//傳送資訊操作

TextMessage(phone_Num, null, phone_sms, pendingIntent, null);*/

//吐司的效果

Text(, "簡訊傳送成功",TH_LONG)();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater()ate(, menu);

return true;

}

}