Implementing a Count down timer using Service in the background

前端 未结 1 1817
梦如初夏
梦如初夏 2021-02-09 04:53

What I want to do in my app: 1. When the user opens the app, he will be able to see an already running countdown timer. So in that case I want to show the countdown numbers in a

1条回答
  •  无人及你
    2021-02-09 05:13

    use bindService(Intent service, ServiceConnection conn, int flag) to bind your countdown service, then in your service, return your binder that contain your count, and in your activity, a instantiated ServiceConnection object can handle the binder. AIDL can do this as well, suggest have a look at how to bind service firstly, wish can help you.

    demo

    public class BindService extends Service{
    private int count;
    private boolean quit;
    private MyBinder binder = new MyBinder();
    // My Binder
    public class MyBinder extends Binder
    {
        public int getCount()
        {
            // get the counting status:count
            return count;
        }
    }
    
    @Override
    public IBinder onBind(Intent intent)
    {
        System.out.println("Service is Binded");
        // return the binder instance
        return binder;
    }
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        System.out.println("Service is Created");
        // counting work
        new Thread()
        {
            @Override
            public void run()
            {
                while (!quit)
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                    }
                    count++;
                }
            }
        }.start();      
    }
    // invoke when the service unbind
    @Override
    public boolean onUnbind(Intent intent)
    {
        System.out.println("Service is Unbinded");
        return true;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        this.quit = true;
        System.out.println("Service is Destroyed");
    }
    
    @Override
    public void onRebind(Intent intent) 
    {
        super.onRebind(intent);
        this.quit = true;
        System.out.println("Service is ReBinded");
    }   
    

    }

    and then the activity

    public class MainActivity extends Activity{
    Button bind , unbind , getServiceStatus;
    BindService.MyBinder binder;
    // define a ServiceConnection object
    private ServiceConnection conn = new ServiceConnection()
    {
        // then the Activity connected with the Service, this will be called
        @Override
        public void onServiceConnected(ComponentName name
            , IBinder service)
        {
            System.out.println("--Service Connected--");
            // achieve MyBinder instance
            binder = (BindService.MyBinder) service;
        }
        // then the connection break off
        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            System.out.println("--Service Disconnected--");         
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        bind = (Button) findViewById(R.id.bind);
        unbind = (Button) findViewById(R.id.unbind);
        getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
    
        final Intent intent = new Intent();
    
        intent.setAction("org.crazyit.service.BIND_SERVICE");       
        bind.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //bind Serivce
                bindService(intent , conn , Service.BIND_AUTO_CREATE);  
            }
        });
        unbind.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //unbind Serivce
                unbindService(conn);
            }
        }); 
        getServiceStatus.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                // Toast to show the conut value
                Toast.makeText(MainActivity.this
                    , "Serivce's count value is:" + binder.getCount()
                    , 4000)
                    .show();
            }
        });
    }}
    

    0 讨论(0)
提交回复
热议问题