Android发送短信、打电话、发送邮件的程序集合。

短信发送模式包括:
1.使用SMSManager发送短信,发送的短信不存于“信息”中。
2.使用ContentResolver发送短信,短信存放于“信息”中。(网传的方法,实践中未成功)
3.使用Intent发送短信,调用系统的“信息”程序发送。

打电话模式包括:
1.调用空的Dial拔号。
2.调用Dial并传递号码。
3.直拔。

发送邮件包括:
1.发送普通邮件。
2.发送附件。

 

[java]  view plain copy
  1.  package lab.sodino.stm;    
  2.  import android.app.Activity;    
  3.  import android.content.ContentResolver;    
  4.  import android.content.ContentValues;    
  5.  import android.content.Intent;    
  6.  import android.net.Uri;    
  7.  import android.os.Bundle;    
  8.  import android.telephony.gsm.SmsManager;    
  9.  import android.view.View;    
  10.  import android.widget.Button;    
  11.  import android.widget.Toast;    
  12.  public class STMAct extends Activity {    
  13.      /** Called when the activity is first created. */    
  14.      @Override    
  15.      public void onCreate(Bundle savedInstanceState) {    
  16.          super.onCreate(savedInstanceState);    
  17.          setContentView(R.layout.main);    
  18.          ((Button) findViewById(R.id.btnSmsMag))    
  19.                  .setOnClickListener(new Button.OnClickListener() {    
  20.                      public void onClick(View v) {    
  21.                          sendSms1();    
  22.                          Toast.makeText(STMAct.this"已发送", Toast.LENGTH_SHORT)    
  23.                                  .show();    
  24.                      }    
  25.                  });    
  26.          ((Button) findViewById(R.id.btnSmsInbox))    
  27.                  .setOnClickListener(new Button.OnClickListener() {    
  28.                      public void onClick(View v) {    
  29.                          sendSmsInbox();    
  30.                          Toast.makeText(STMAct.this"已发送", Toast.LENGTH_SHORT)    
  31.                                  .show();    
  32.                      }    
  33.                  });    
  34.          ((Button) findViewById(R.id.btnSmsIntent))    
  35.                  .setOnClickListener(new Button.OnClickListener() {    
  36.                      public void onClick(View v) {    
  37.                          sendSmsIntent();    
  38.                          Toast.makeText(STMAct.this"已发送", Toast.LENGTH_SHORT)    
  39.                                  .show();    
  40.                      }    
  41.                  });    
  42.          ((Button) findViewById(R.id.btnTelEmpty))    
  43.                  .setOnClickListener(new Button.OnClickListener() {    
  44.                      public void onClick(View v) {    
  45.                          telDialEmpty();    
  46.                      }    
  47.                  });    
  48.          ((Button) findViewById(R.id.btnTelPhone))    
  49.                  .setOnClickListener(new Button.OnClickListener() {    
  50.                      public void onClick(View v) {    
  51.                          telDialPhone();    
  52.                      }    
  53.                  });    
  54.          ((Button) findViewById(R.id.btnTelCall))    
  55.                  .setOnClickListener(new Button.OnClickListener() {    
  56.                      public void onClick(View v) {    
  57.                          telCall();    
  58.                      }    
  59.                  });    
  60.          ((Button) findViewById(R.id.btnMailSendto))    
  61.                  .setOnClickListener(new Button.OnClickListener() {    
  62.                      public void onClick(View v) {    
  63.                          mailSendto();    
  64.                      }    
  65.                  });    
  66.          ((Button) findViewById(R.id.btnMailSend))    
  67.                  .setOnClickListener(new Button.OnClickListener() {    
  68.                      public void onClick(View v) {    
  69.                          mailSend();    
  70.                      }    
  71.                  });    
  72.      }    
  73.      private void sendSms1() {    
  74.          // 需要 android.permission.SEND_SMS    
  75.          SmsManager smsManager = SmsManager.getDefault();    
  76.          smsManager.sendTextMessage("10086"null"1008611"nullnull);    
  77.      }    
  78.      private void sendSmsInbox() {    
  79.          // 需要 android.permission.READ_SMS与android.permission.WRITE_SMS,经测试发送失败  
  80.          ContentValues values = new ContentValues();    
  81.          values.put("address""10086");    
  82.          values.put("body""bylcx");    
  83.          ContentResolver contentResolver = getContentResolver();    
  84.          // 实验中两者都会在信息栏中保存所发的信息。    
  85.          contentResolver.insert(Uri.parse("content://sms/sent"), values);    
  86.          // contentResolver.insert(Uri.parse("content://sms/inbox"), values);    
  87.      }    
  88.      private void sendSmsIntent() {    
  89.          // 不需要权限,跳转到"信息"中。    
  90.          Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri    
  91.                  .parse("sms://"));    
  92.          sendIntent.putExtra("address""10086");    
  93.          sendIntent.putExtra("sms_body""bylcs");    
  94.          startActivity(sendIntent);    
  95.      }    
  96.      private void telDialEmpty() {    
  97.          // 不需要权限,跳转到"拔号"中。    
  98.          Intent callIntent = new Intent(Intent.ACTION_CALL_BUTTON);    
  99.          startActivity(callIntent);    
  100.      }    
  101.      private void telDialPhone() {    
  102.          // 不需要权限,跳转到"拔号"中。    
  103.          Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri    
  104.                  .parse("tel:10086"));    
  105.          startActivity(callIntent);    
  106.      }    
  107.      private void telCall() {    
  108.          // 需要 android.permission.CALL_PHONE    
  109.          Intent callIntent = new Intent(Intent.ACTION_CALL, Uri    
  110.                  .parse("tel:10086"));    
  111.          startActivity(callIntent);    
  112.      }    
  113.      private void mailSendto() {    
  114.          // 需要 android.permission.SENDTO权限    
  115.          Uri uri = Uri.parse("mailto:10086@qq.com");    
  116.          Intent mailIntent = new Intent(Intent.ACTION_SENDTO, uri);    
  117.          startActivity(mailIntent);    
  118.      }    
  119.      private void mailSend() {    
  120.          // 需要 android.permission.SEND权限    
  121.          Intent mailIntent = new Intent(Intent.ACTION_SEND);    
  122.          // 可以试下“plain/text”与“text/plain”的区别,嘿嘿  
  123.          mailIntent.setType("plain/text");    
  124.          String[] arrReceiver = { "10086@qq.com""10086@qq.com" };    
  125.          String[] arrCc = { "10086@qq.com""10086@qq.com" };    
  126.          String[] arrBcc = { "10086@qq.com""10086@qq.com" };    
  127.          String mailSubject = "MailSubject";    
  128.          String mailBody = "Mail Sodino Test";    
  129.          String attachPath = "file:///sdcard/UCDownloads/ATest.apk";    
  130.          mailIntent.putExtra(Intent.EXTRA_EMAIL, arrReceiver);    
  131.          mailIntent.putExtra(Intent.EXTRA_CC, arrCc);    
  132.          mailIntent.putExtra(Intent.EXTRA_BCC, arrBcc);    
  133.          mailIntent.putExtra(Intent.EXTRA_SUBJECT, mailSubject);    
  134.          mailIntent.putExtra(Intent.EXTRA_TEXT, mailBody);    
  135.          mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachPath));    
  136.          mailIntent.setType("audio/mp3");    
  137.          startActivity(Intent.createChooser(mailIntent, "Mail Sending..."));    
  138.      }    
  139. }  
 

 

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:text="SMS"  
  9.     android:textSize="30sp"  
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"  
  12.     android:gravity="center"  
  13.     />  
  14. <LinearLayout  
  15.     android:orientation="horizontal"  
  16.     android:layout_width="fill_parent"  
  17.     android:layout_height="wrap_content"  
  18.     android:gravity="center"  
  19.     android:background="#80808080">  
  20.     <Button  
  21.         android:text="SMSMag"  
  22.         android:id="@+id/btnSmsMag"  
  23.         android:gravity="center"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.     >Button>  
  27.     <Button  
  28.         android:text="Inbox"  
  29.         android:id="@+id/btnSmsInbox"  
  30.         android:gravity="center"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.     >Button>  
  34.     <Button  
  35.         android:text="Intent"  
  36.         android:id="@+id/btnSmsIntent"  
  37.         android:gravity="center"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.     >Button>  
  41. LinearLayout>  
  42. <TextView  
  43.     android:text="TEL"  
  44.     android:textSize="30sp"  
  45.     android:layout_width="fill_parent"  
  46.     android:layout_height="wrap_content"  
  47.     android:gravity="center"  
  48. >TextView>  
  49. <LinearLayout  
  50.     android:orientation="horizontal"  
  51.     android:layout_width="fill_parent"  
  52.     android:layout_height="wrap_content"  
  53.     android:gravity="center"  
  54.     android:background="#80808080">  
  55.     <Button  
  56.         android:text="EmptyDial"  
  57.         android:id="@+id/btnTelEmpty"  
  58.         android:layout_width="wrap_content"  
  59.         android:layout_height="wrap_content"  
  60.         android:gravity="center"  
  61.     >Button>  
  62.     <Button  
  63.         android:text="PhoneDial"  
  64.         android:id="@+id/btnTelPhone"  
  65.         android:layout_width="wrap_content"  
  66.         android:layout_height="wrap_content"  
  67.         android:gravity="center"  
  68.     >Button>  
  69.     <Button  
  70.         android:text="Call"  
  71.         android:id="@+id/btnTelCall"  
  72.         android:layout_width="wrap_content"  
  73.         android:layout_height="wrap_content"  
  74.         android:gravity="center"  
  75.     >Button>  
  76. LinearLayout>  
  77. <TextView  
  78.     android:text="MAIL"  
  79.     android:textSize="30sp"  
  80.     android:layout_width="fill_parent"  
  81.     android:layout_height="wrap_content"  
  82.     android:gravity="center"  
  83. >TextView>  
  84. <LinearLayout  
  85.     android:orientation="horizontal"  
  86.     android:layout_width="fill_parent"  
  87.     android:layout_height="wrap_content"  
  88.     android:gravity="center"  
  89.     android:background="#80808080">  
  90.     <Button  
  91.         android:text="SendTo"  
  92.         android:id="@+id/btnMailSendto"  
  93.         android:layout_width="wrap_content"  
  94.         android:layout_height="wrap_content"  
  95.         android:gravity="center"  
  96.     >Button>  
  97.     <Button  
  98.         android:text="Send(Attach)"  
  99.         android:id="@+id/btnMailSend"  
  100.         android:layout_width="wrap_content"  
  101.         android:layout_height="wrap_content"  
  102.         android:gravity="center"  
  103.     >Button>  
  104. LinearLayout>  
  105. LinearLayout>  

如何控制接收者或者发送方得权限?

       1)如果广播发送方要求接收方必须有某个权限才能收到广播怎么做呢?

       /**

        * 发送广播,指定接收者权限

        * sendBroadcast(i, "com.iteye.permission.receiver");//指定接收者权限

        */

       public void sendBroadcast() {

              //隐式意图,发送广播

              Intent i = new Intent();

              i.setAction("com.iteye.receiver.action");

              i.putExtra("name", "tom");

              this.sendBroadcast(i, " com.iteye.permission.receiver ");

              Log.i("Other",".send ok!");

       }

       在清单文件里receiver必须有这个权限才能收到广播。

       首先,需要定义权限:

       然后,声明权限:

       这时接收者就能收到发送的广播。

       2)反过来,如果接收者要求发送方必须拥有某个权限,我才接收你的广播怎么办呢?

       

     

               

                      

                      

               

      

       即使过滤器匹配,如果发送方没有相应权限,接收者也不会接收其广播。

更多相关文章

  1. Google Android如何分析和研究Log文件 ,如何看日志信息
  2. Android短信息验证码自动填写详细介绍
  3. android api code学习之ActivityManager
  4. Android(安卓)禁止安装没有授权的第三方应用
  5. android 系统权限大全的简介与内容
  6. Android之在ubuntu上用aapt查看apk的名字以及相关信息
  7. 关于android:sharedUserId="android.uid.system"这个系统级权限
  8. Android信息推送—AndroidPN的学习(上)
  9. Android(安卓)Native程序crash的一些定位方法简介

随机推荐

  1. 打造Android万能适配器Adapter
  2. android 使用opengl画简单的图形
  3. Android中文API(145) —— NotificationMan
  4. Android ApiDemos示例解析(159):Views->L
  5. [Android] 仿网易新闻客户端分类排序
  6. android 模拟器 3D 开发环境配置
  7. android PopupWindow 浅析
  8. MTP模式与USB存储模式(MTP in Android)
  9. Android(安卓)View创建和销毁调用的所有
  10. 如何查看USB方式连接Android设备的外接设