栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android中使用Service实现后台发送邮件功能实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android中使用Service实现后台发送邮件功能实例

本文实例讲述了Android中使用Service实现后台发送邮件功能。分享给大家供大家参考,具体如下:

程序如下:

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.widget.TextView;
public class A05Activity extends Activity {
 private TextView tv;
 private String[] receiver;
 private String subject;
 private String body;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.tv);
    tv.setText("等待接收邮件中···");
    try {
     //取得短信传来的Bundle
  Bundle b=this.getIntent().getExtras();
  if(b!=null){
  //将Bundle中的字符串取出
  String s=b.getString("input");
  receiver=new String[]{"1650967185@163.com"};
  subject="邮箱发送";
  body=s.toString();
  //自定义一个Intent业执行发送E-mail的工作
  Intent i=new Intent(android.content.Intent.ACTION_SEND);
  i.setType("plain/text");//设置邮件格式为“plain/text”
  i.putExtra(android.content.Intent.EXTRA_EMAIL,receiver);//传入收件人地址
  i.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);//传入邮件主题
  i.putExtra(android.content.Intent.EXTRA_TEXT, body);//传入邮件内容
  startActivity(Intent.createChooser(i, getResources().getString(R.string.message)));
  }
  else{
  finish();
  }
 } catch (NotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
  }
}

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;//用来收取短信
import android.widget.Toast;//告知用户收到短信
@SuppressWarnings("deprecation")
public class ServiceA05 extends BroadcastReceiver{
 public static final String mAction="android.provider.Telephony.SMS_RECEIVED";
  private String str_receiver="收到短信";
 @Override
 public void onReceive(Context arg0, Intent arg1) {
 // TODO Auto-generated method stub
 Toast.makeText(arg0, str_receiver.toString(), Toast.LENGTH_LONG).show();
 if(arg1.getAction().equals(mAction)){
  //建构一字符串集合变量sb
  StringBuilder sb=new StringBuilder();
  //接收数据
  Bundle b=arg1.getExtras();
  //判断intent传送数据是否为空
  if(b!=null){
  //pdus为android内置的短信参数indentifier
  
  Object[] myOBJpuds=(Object[])b.get("pdus");
  //构造短信对象数组,并根据短信内容大小来确定数组的大小
  SmsMessage[] sm=new SmsMessage[myOBJpuds.length];
  for(int i=0;i

AndroidManifest.xml如下:



  
  
    
      
 
 
      
    
    
      
 
      
    
  
  

在android中注册一个BroadcastReceiver,并设置这个receiver的intent-filter(Android.provider.Telephony.SMS_RECEIVED),让它针对短信事件做出反应。并且还要添加一个权限:android.permission.RECEIVE_SMS。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》及《Android开发入门与进阶教程》

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号