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