Service, BroadcastReceiver, and Activity can communicate with each other. Here we use an example to see one of them.
I. Functions
First, you should know that operations on the UI on a non-UI interface will result in errors. That is to say, it is illegal to operate the UI in the main thread on a thread other than the main thread, but we can do the following, to avoid this situation.
Specific functions: On the main interface, start the background Service to process data in the background. The foreground obtains data and updates the UI through BroadcastReceiver.
II. Specific implementation
1) create a class inherited from the Service to overwrite the corresponding method and process data.
Public class MyService extends Service {private Timer timer; // declare the Timer private TimerTask task; // declare the timer Task private int index = 101; // countdown start variable @ Override public IBinder onBind (Intent intent) {return null ;}@ Override public void onCreate () {super. onCreate () ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {startTimer (); // start the timer return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {stopTimer (); // stop the timer super. onDestroy ();} public void startTimer () {timer = new Timer (); // instantiate the Timer object task = new TimerTask () {// instantiate TimerTask @ Override public void run () {index --; Intent i1 = new Intent (); // The page cannot be redirected, i1.setAction ("action111"); // sets the action attribute of the Intent object to facilitate matching i1.putExtra ("name", index) on the main interface ); // carry data sendBroadcast (i1); // send broadcast}; timer. schedule (task, 1000,100 0); // start timer} // stop timer public void stopTimer () {timer. cancel ();}} |
2) register the Service in the Minifest. xml file
<service android:name="MyService"></service> |
3) start the Service, accept the data, and update the UI on the main interface.
Public class MainActivity extends Activity implements OnClickListener {private Button startBtn, stopBtn; // declare the buttons on the main interface private Intent intent; // declare the Intent object private TextView TV; // declare the display area of the main interface // create a BroadcastReceiver object to accept messages sent by the Service and process private BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Override public void onReceive (Context context, intent intent) {int num = intent. getIntExtra ("name", 0); // obtain the TV value for which the key value is name. setText (num + ""); // update the UI of the main interface}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // find the resource startBtn = (Button) findViewById (R. id. start); stopBtn = (Button) findViewById (R. id. stop); // load the listener startBtn for the button. setOnClickListener (this); stopBtn. setOnClickListener (this); // find the resource TV = (TextView) findViewById (R. id. textView1); // dynamically register the BroadcastReceiver object and add the action to receive the data. The action is matched and the data is processed successfully. Otherwise, the registerReceiver (receiver er, new IntentFilter ("action111") is not processed ")); // instantiate the Intent object intent = new Intent (MainActivity. this, MyService. class) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. start: startService (intent); // start Service break; case R. id. stop: stopService (intent); // stop Service break; }}@ Override protected void onStop () {unregisterReceiver (receiver); // cancel Dynamic Registration of super. onStop ();}} |
4) Results
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10343024T-0.png "title =" .png "/>
5) Result Analysis:
The main process of the program here is to process data in the Service, and then pass the data through the Intent object to the front-end, and receive the data and update the UI at the front-end through BroadcastReceiver. We can clearly see the connection and joint use between the three. In future programming, we may also use similar functions. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/103430CB-1.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1287158