Android service and broadcast with demo

Source: Internet
Author: User

Package Com.example.yqqmobilesafe.service;import Java.util.list;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.app.service;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.asynctask;import Android.os.ibinder;import Android.telephony.smsmessage;import Android.util.log;import Com.example.yqqmobilesafe.rubishsmsactivity;import Com.example.yqqmobilesafe.contactprovider.contactinfoprovider;import Com.example.yqqmobilesafe.db.dao.rubishsmsinfodao;import Com.example.yqqmobilesafe.domain.contactinfo;import com.example.yqqmobilesafe.domain.rubishsmsinfo;/** * Unfamiliar SMS Interception service * @author YQQ * */public class Stopstrangerphonenumbersmsservice extends Service {Private Boolean d=true;private stopstrangersmsreceiver mreceiver;/ /Stranger SMS Intercept Broadcast recipient public Stopstrangerphonenumbersmsservice () {//TODO auto-generated constructor stub} @OverridepUblic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {if (D) {log.i ("Stopstrangerphonenumbersmsservice", "OnCreate ()");} Registered broadcast mreceiver=new stopstrangersmsreceiver (); Intentfilter filter=new intentfilter (); Filter.setpriority ( Integer.max_value); Filter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Registerreceiver (Mreceiver, filter); Super.oncreate ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {if (D) {log.i (" Stopstrangerphonenumbersmsservice "," Onstartcommand ");} Return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {if (D) {log.i ("Stopstrangerphonenumbersmsservice", "OnDestroy ()");} Cancel broadcast recipient unregisterreceiver (Mreceiver); Mreceiver=null;super.ondestroy ();} Stranger SMS Intercept Broadcast recipient class Stopstrangersmsreceiver extends Broadcastreceiver{private string sender;//sender number private string body;//SMS Content @overridepublic void OnReceive (context context, Intent Intent) {object[] Objs = (object[]) Intent.getextras (). Get ("PDUs"); for (Object obj:objs) {smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU ((byte[)) obj) ///Get the sender's number of the SMS sender = Smsmessage.getoriginatingaddress (); BODY = Smsmessage.getmessagebody (); New Asynctask<void,void,void> () {list<contactinfo> infos=null; @Overrideprotected Void Doinbackground ( Void ... params) {Contactinfoprovider contactinfoprovider=new Contactinfoprovider (Getapplicationcontext ()); infos= Contactinfoprovider.getsystemcontactinfos (); return null;} @Overrideprotected void OnPostExecute (void result) {if (infos==null) {//toast.maketext (Getapplicationcontext (), Duration) return;} for (ContactInfo Info:infos) {if (Info.getphonenumber (). Equals (sender)) {return;}} Abortbroadcast (); new asynctask<void,void,void> () {@Overrideprotected void Doinbackground (void ... params) { Rubishsmsinfo sms=new rubishsmsinfo (); Sms.setphonenumber (sender); sms.setsmscontent (body);// Storing strangers ' text messages in the database table Rubishsmsinfodao rubishsmsinfodao=new Rubishsmsinfodao (Getapplicationcontext ()); RubiShsmsinfodao.addsmsinfos (SMS); return null;} @Overrideprotected void OnPostExecute (void result) {//TODO auto-generated method Stubsuper.onpostexecute (Result); Notificationmanager nm= (Notificationmanager) Getsystemservice (notification_service); Intent intent=new Intent ( Stopstrangerphonenumbersmsservice.this,rubishsmsactivity.class); Pendingintent pi=pendingintent.getactivity (stopstrangerphonenumbersmsservice.this, 0, intent, 0);// Send an interception notice notification notification=new Notification.builder (Getapplicationcontext ()). Setautocancel (True). Setticker ("Intercept to unfamiliar text messages"). Setsmallicon (com.example.yqqmobilesafe.r.drawable.notif_icon_sms). Setcontenttext ("Intercept to unfamiliar text messages"). Setcontentintent (PI). build (); Nm.notify (100,notification);}}. Execute (); Super.onpostexecute (result);}}. Execute ();}}}

Android service and broadcast with demo

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.