Service service---------to receive broadcasts

Source: Internet
Author: User
Tags stub

1, when we need to listen to the mobile app in the operation of a certain broadcast, do a corresponding operation, then we can register in the XML broadcast and then listen, but then our app is the whole process of listening to this broadcast. If our needs are changed to a certain interface of the app, then we need to listen to this broadcast ... So how do we deal with, dynamic to register this broadcast, when the interface is closed to log off this broadcast monitoring. This will solve the problem. But if our needs are across several interfaces, we still need to listen. So how to deal with, is it in every interface to register and then logout ... So it's too low. But if the service is combined with the broadcast, it will be good to deal with these problems. Turn on the service to listen to the broadcast when an interface is turned on, and then shut down the service when the interface is closed. You can avoid duplicate creation by writing the broadcast listener in the service. Here's an example, but it's not the third case I'm talking about, but it's registered in the service:


2. Broadcast recipients:

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;

public class Screenreceiver extends Broadcastreceiver {

	@Override public
	void OnReceive (context context, Intent Intent) {
//		System.out.println ("I was called Oh");		 <action android:name= "Android.intent.action.SCREEN_ON"/>
//       <action android:name= " Android.intent.action.SCREEN_OFF "/>
		
		String action = intent.getaction ();
		if (Action.equals ("Android.intent.action.SCREEN_OFF")) {
			System.out.println ("The screen is off, oh");
		}
		if (Action.equals ("Android.intent.action.SCREEN_ON")) {
			System.out.println ("The screen opens Yo");}}}



3. Service:

Import Android.app.Service;
Import android.content.Intent;
Import Android.content.IntentFilter;

Import Android.os.IBinder;
	public class Screenservice extends Service {private screenreceiver receiver;
	@Override public IBinder onbind (Intent Intent) {//TODO auto-generated method stub return null;
		} @Override public void OnCreate () {super.oncreate ();
		
Receiver = new Screenreceiver (); <intent-filter >//<action android:name= "Android.intent.action.SCREEN_ON"/>//<actio n android:name= "Android.intent.action.SCREEN_OFF"/>//</intent-filter> intentfilter filter=new Intentfil
		ter ();
		Filter.addaction ("Android.intent.action.SCREEN_ON");
		Filter.addaction ("Android.intent.action.SCREEN_OFF");
	Dynamic use of code to register the broadcast receiver//register the broadcast receiver with the code does not need to register the Registerreceiver (receiver, filter) in the manifest file;
		
		} @Override public void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy (); Unregisters the broadcast recipient Unregisterreceiver when the service is closed (Receiver); }

}


4. Call in Mainactivity:

public class Mainactivity extends Activity {

	@Override
	protected void onCreate (Bundle savedinstancestate) {
		super.oncreate (savedinstancestate);
		Setcontentview (r.layout.activity_main);
		
		Intent service=new Intent ();
		Service.setclass (this, screenservice.class);
		StartService (service);
	}

}

Of course I did not write down this service here, you can close the relationship when you want to shut down.

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.