Android傳送簡訊、打電話、發送郵件的程式集合。
簡訊發送模式包括:
1.使用SMSManager傳送簡訊,發送的簡訊不存於“資訊”中。
2.使用ContentResolver傳送簡訊,簡訊存放於“資訊”中。(網傳的方法,實踐中未成功)
3.使用Intent傳送簡訊,調用系統的“資訊”程式發送。
打電話模式包括:
1.調用空的Dial拔號。
2.調用Dial並傳遞號碼。
3.直拔。
發送郵件包括:
1.發送普通郵件。
2.發送附件。
package lab.sodino.stm;<br /> import android.app.Activity;<br /> import android.content.ContentResolver;<br /> import android.content.ContentValues;<br /> import android.content.Intent;<br /> import android.net.Uri;<br /> import android.os.Bundle;<br /> import android.telephony.gsm.SmsManager;<br /> import android.view.View;<br /> import android.widget.Button;<br /> import android.widget.Toast;<br /> public class STMAct extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> ((Button) findViewById(R.id.btnSmsMag))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> sendSms1();<br /> Toast.makeText(STMAct.this, "已發送", Toast.LENGTH_SHORT)<br /> .show();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnSmsInbox))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> sendSmsInbox();<br /> Toast.makeText(STMAct.this, "已發送", Toast.LENGTH_SHORT)<br /> .show();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnSmsIntent))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> sendSmsIntent();<br /> Toast.makeText(STMAct.this, "已發送", Toast.LENGTH_SHORT)<br /> .show();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnTelEmpty))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> telDialEmpty();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnTelPhone))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> telDialPhone();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnTelCall))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> telCall();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnMailSendto))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> mailSendto();<br /> }<br /> });<br /> ((Button) findViewById(R.id.btnMailSend))<br /> .setOnClickListener(new Button.OnClickListener() {<br /> public void onClick(View v) {<br /> mailSend();<br /> }<br /> });<br /> }<br /> private void sendSms1() {<br /> // 需要 android.permission.SEND_SMS<br /> SmsManager smsManager = SmsManager.getDefault();<br /> smsManager.sendTextMessage("10086", null, "1008611", null, null);<br /> }<br /> private void sendSmsInbox() {<br /> // 需要 android.permission.READ_SMS與android.permission.WRITE_SMS,經測試發送失敗<br /> ContentValues values = new ContentValues();<br /> values.put("address", "10086");<br /> values.put("body", "bylcx");<br /> ContentResolver contentResolver = getContentResolver();<br /> // 實驗中兩者都會在資訊列中儲存所發的資訊。<br /> contentResolver.insert(Uri.parse("content://sms/sent"), values);<br /> // contentResolver.insert(Uri.parse("content://sms/inbox"), values);<br /> }<br /> private void sendSmsIntent() {<br /> // 不需要許可權,跳轉到"資訊"中。<br /> Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri<br /> .parse("sms://"));<br /> sendIntent.putExtra("address", "10086");<br /> sendIntent.putExtra("sms_body", "bylcs");<br /> startActivity(sendIntent);<br /> }<br /> private void telDialEmpty() {<br /> // 不需要許可權,跳轉到"拔號"中。<br /> Intent callIntent = new Intent(Intent.ACTION_CALL_BUTTON);<br /> startActivity(callIntent);<br /> }<br /> private void telDialPhone() {<br /> // 不需要許可權,跳轉到"拔號"中。<br /> Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri<br /> .parse("tel:10086"));<br /> startActivity(callIntent);<br /> }<br /> private void telCall() {<br /> // 需要 android.permission.CALL_PHONE<br /> Intent callIntent = new Intent(Intent.ACTION_CALL, Uri<br /> .parse("tel:10086"));<br /> startActivity(callIntent);<br /> }<br /> private void mailSendto() {<br /> // 需要 android.permission.SENDTO許可權<br /> Uri uri = Uri.parse("mailto:10086@qq.com");<br /> Intent mailIntent = new Intent(Intent.ACTION_SENDTO, uri);<br /> startActivity(mailIntent);<br /> }<br /> private void mailSend() {<br /> // 需要 android.permission.SEND許可權<br /> Intent mailIntent = new Intent(Intent.ACTION_SEND);<br /> // 可以試下“plain/text”與“text/plain”的區別,嘿嘿<br /> mailIntent.setType("plain/text");<br /> String[] arrReceiver = { "10086@qq.com", "10086@qq.com" };<br /> String[] arrCc = { "10086@qq.com", "10086@qq.com" };<br /> String[] arrBcc = { "10086@qq.com", "10086@qq.com" };<br /> String mailSubject = "MailSubject";<br /> String mailBody = "Mail Sodino Test";<br /> String attachPath = "file:///sdcard/UCDownloads/ATest.apk";<br /> mailIntent.putExtra(Intent.EXTRA_EMAIL, arrReceiver);<br /> mailIntent.putExtra(Intent.EXTRA_CC, arrCc);<br /> mailIntent.putExtra(Intent.EXTRA_BCC, arrBcc);<br /> mailIntent.putExtra(Intent.EXTRA_SUBJECT, mailSubject);<br /> mailIntent.putExtra(Intent.EXTRA_TEXT, mailBody);<br /> mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachPath));<br /> mailIntent.setType("audio/mp3");<br /> startActivity(Intent.createChooser(mailIntent, "Mail Sending..."));<br /> }<br />}
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br />android:text="SMS"<br />android:textSize="30sp"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:gravity="center"<br /> /><br /><LinearLayout<br />android:orientation="horizontal"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />android:background="#80808080"><br /><Button<br />android:text="SMSMag"<br />android:id="@+id/btnSmsMag"<br />android:gravity="center"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />></Button><br /><Button<br />android:text="Inbox"<br />android:id="@+id/btnSmsInbox"<br />android:gravity="center"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />></Button><br /><Button<br />android:text="Intent"<br />android:id="@+id/btnSmsIntent"<br />android:gravity="center"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />></Button><br /></LinearLayout><br /><TextView<br />android:text="TEL"<br />android:textSize="30sp"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></TextView><br /><LinearLayout<br />android:orientation="horizontal"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />android:background="#80808080"><br /><Button<br />android:text="EmptyDial"<br />android:id="@+id/btnTelEmpty"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></Button><br /><Button<br />android:text="PhoneDial"<br />android:id="@+id/btnTelPhone"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></Button><br /><Button<br />android:text="Call"<br />android:id="@+id/btnTelCall"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></Button><br /></LinearLayout><br /><TextView<br />android:text="MAIL"<br />android:textSize="30sp"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></TextView><br /><LinearLayout<br />android:orientation="horizontal"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />android:background="#80808080"><br /><Button<br />android:text="SendTo"<br />android:id="@+id/btnMailSendto"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></Button><br /><Button<br />android:text="Send(Attach)"<br />android:id="@+id/btnMailSend"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:gravity="center"<br />></Button><br /></LinearLayout><br /></LinearLayout><br />