Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How android studio binds services and threads to implement timers

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how android studio binds services and threads to implement timers, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Experimental purpose:

Familiar with and master the use of Android threads

Experimental requirements:

1. Complete a stopwatch with start and stop function

two。 Implement the function by binding the service and update the interface through Thread+handler

This chapter doesn't take much time to learn, and there are a lot of other things, so it's just a simple implementation, and there's still something left unhandled in the life cycle, so

The main idea is: start a thread in the service to implement the counting function, and call the function to update the interface every 10ms, which requires Thread+handler, and of course, some public functions that control the start and stop are called by activity. In the same way as the bound service, the instance of the service can be obtained in activity, so activity is used as the controller to call the service control start and stop function or count zero function for different button events. In order to realize the function of timer. After completing the experiment, it is found that the precision of the timer implemented in this way is relatively rough, but the function is normal, and a better idea is to use the time function, but the purpose of this experiment is to practice the use of threads and binding services, so it has not been changed.

Lab code:

MyService .java

Package com.example.shiyan5;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class MyService extends Service {private final IBinder binder = new MyBinder (); private Thread workThread; private int count=0; private boolean clockstop truth; public MyService () {} public void clearcount () {count=0;} public void countstop () {c_stop=true } public void countstart () {clockstop false;} @ Override public void onCreate () {super.onCreate (); workThread=new Thread (null,backgroundWork); workThread.start ();} @ Override public boolean onUnbind (Intent intent) {return super.onUnbind (intent);} public class MyBinder extends Binder {MyService getService () {return MyService.this } @ Override public IBinder onBind (Intent intent) {/ / TODO: Return the communication channel to the service. Return binder; / / throw new UnsupportedOperationException ("Not yet implemented");} private Runnable backgroundWork = new Runnable () {@ Override public void run () {try {while (true) {if (c_stop==false) {count++ } MainActivity.UpdateGUI (count); Thread.sleep (10); / / 10 millisecond count Z}} catch (InterruptedException e) {e.printStackTrace ();}};}

MainActivity.java

Package com.example.shiyan5;import androidx.appcompat.app.AppCompatActivity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity {static TextView textView1,textView2; Button bt_clear,bt_stop,bt_start; MyService mService; boolean mBound; static int count Static Handler handler=new Handler (); private ServiceConnection connection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName className, IBinder service) {MyService.MyBinder binder = (MyService.MyBinder) service; mService = binder.getService (); / / the instance of the service mBound = true;} @ Override public void onServiceDisconnected (ComponentName arg0) {mBound = false }}; public static void UpdateGUI (int s_count) {count=s_count; handler.post (RefreshText);} private static Runnable RefreshText=new Runnable () {@ Override public void run () {String sa,sb,sc; int astat0; if (a)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report