Android uses service to implement background send mail function instance _android

Source: Internet
Author: User

This article describes the use of service in Android to implement the background send mail function. Share to everyone for your reference, specific as follows:

The procedure is as follows:

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; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    tv= (TextView) Findviewbyid (r.id.tv);
    Tv.settext ("Waiting to receive mail");
  try {//Get SMS from Bundle Bundle b=this.getintent (). Getextras ();
  if (b!=null) {//Bundle string s=b.getstring ("input")
  Receiver=new string[]{"1650967185@163.com"};
  subject= "Mailbox Send";
  Body=s.tostring ();
  Customize a Intent industry to perform the work of sending e-mail Intent i=new Intent (Android.content.Intent.ACTION_SEND); I.settype ("Plain/text");//Set Message format to "Plain/text" I.putextra (Android.content.intent.extra_email,receiver);Into the recipient address I.putextra (Android.content.Intent.EXTRA_SUBJECT, SUBJECT);//Incoming message subject I.putextra (Android.content.Intent.EXTRA
  _text, body);//Incoming message content startactivity (Intent.createchooser (i, Getresources (). getString (R.string.message)));
  else{finish ();
 The 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;//is used to receive SMS import android.widget.toast;//inform users to receive SMS @SuppressWarnings (" Deprecation ") public class ServiceA05 extends broadcastreceiver{public static final String maction=" Android.provider.Te
  Lephony.sms_received ";
 Private String str_receiver= "received SMS"; @Override public void OnReceive (context arg0, Intent arg1) {//TODO auto-generated Method Stub Toast.maketext (arg0, St
 R_receiver.tostring (), Toast.length_long). Show ();
  if (Arg1.getaction (). Equals (Maction)) {//construct a string Set variable SB StringBuilder sb=new StringBuilder ();
  Receive Data Bundle B=arg1.getextras (); Determine if the intent transfer data is null if (b!=null) {//pdus for Android built-in SMS parameters indentifier/* Returns an Object containing Bundle.get via PDUs ("") * * * object[]
  Myobjpuds= (object[]) b.get ("PDUs");
  Constructs the short message object array, and according to the text message content size to determine the array size smsmessage[] sm=new smsmessage[myobjpuds.length]; for (int I=0;I<MYOBJPUDS.Length;i++) {SM[I]=SMSMESSAGE.CREATEFROMPDU (byte[]) myobjpuds[i]);
   ///merge SMS with custom information in StringBuilder for (Smsmessage sm01:sm) {Sb.append ("received from: \ n");
   The telephone number of the addressee Sb.append (sm01.getdisplayoriginatingaddress ());
   Sb.append ("\ n--------message---------\ n");
   Obtain the content of the text message Sb.append (Sm01.getdisplaymessagebody ());
  Use Toast to display incoming call information Toast.maketext (arg0, sb.tostring (), Toast.length_long). Show ();
  } toast.maketext (arg0, sb.tostring (), Toast.length_long). Show ();
  Return to the main activity Intent i=new Intent (arg0,a05activity.class);
  Define a Bundle Bundle b01=new Bundle ();
  The text message is deposited in the bundle b01.putstring ("Input", sb.tostring ()) by the putstring () method;
  Put the bundle into the intent I.putextras (B01);
  Set the Intent FLAG to run I.setflags (Intent.flag_activity_new_task) with a new TASK;
 Arg0.startactivity (i);

 }
 }
}

The

Androidmanifest.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.my.a05 "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkve
    Rsion= "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <activity android:name= ". A05activity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Andr Oid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent -filter> </activity> <receiver android:name= "ServiceA05" > <intent-filter> <a ction android:name= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
 ;/application> <uses-permission android:name= "Android.permission.RECEIVE_SMS"/> </manifest>

Register a broadcastreceiver in Android and set up this receiver Intent-filter (Android.provider.Telephony.SMS_RECEIVED), Let it respond to SMS events. And also add a permission: Android.permission.RECEIVE_SMS.

More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.