Android實現發簡訊/多媒體訊息功能

來源:互聯網
上載者:User

 首先 activity_main.xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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=".SmsActivity" >

<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" >

 

</EditText>

<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" />

 

</RelativeLayout>


然後在AndroidManifest.xml 中添加 傳送簡訊的許可權:
<uses-permission android:name="android.permission.SEND_SMS"/>

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

public class SmsActivity extends Activity {


// 聲明控制項對象
private Button butten_sms;
private EditText edit_phone;
private EditText edit_sms;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設定顯示視圖
setContentView(R.layout.activity_sms);

//擷取按鈕組件
butten_sms=(Button) findViewById(R.id.But_sms);

//擷取輸入框組件
edit_phone = (EditText) findViewById(R.id.edit_phone);

edit_sms = (EditText) findViewById(R.id.edit_sms);

//註冊按鈕事件
butten_sms.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
/**
* 發多媒體訊息
*/
//擷取電話號
String phone_num = edit_phone.getText().toString();

//建立意圖對象
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/a.jpg"));
intent.putExtra("address", phone_num);
intent.putExtra("exit_on_sent", true);
intent.putExtra("subject", "subject:I love you");
intent.putExtra("sms_body", "content ::XXXX");
intent.setType("image/jpeg");


startActivity(intent);

/*// 擷取簡訊管理器對象
SmsManager smsManager = SmsManager.getDefault();
// 意圖對象
PendingIntent pendingIntent = PendingIntent.getBroadcast(
SmsActivity.this, 0, new Intent(), 0);*/

}
});
}


//發簡訊 功能
public void send(){
//擷取號碼
String phone_Num = edit_phone.getText().toString();

//擷取發送的簡訊
String phone_sms = edit_sms.getText().toString();

//擷取資訊管理器對象
SmsManager smsmanager = SmsManager.getDefault();

//意圖對象
PendingIntent pendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, new Intent(), 0);

//判斷 資訊長度 大於 70則分兩條發送。。預設是70
ArrayList<String> contents = smsmanager
.divideMessage(phone_sms);
for (String c : contents) {
// 發送資訊的操作
smsmanager.sendTextMessage(phone_Num, null, c,
pendingIntent, null);
}

/*//發送資訊操作
smsmanager.sendTextMessage(phone_Num, null, phone_sms, pendingIntent, null);*/

//多士的效果
Toast.makeText(SmsActivity.this, "簡訊發送成功",Toast.LENGTH_LONG).show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sms, menu);
return true;
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.