Android Local Service Sample, bindservice(), and ServiceConnection()

前端 未结 2 1582
无人共我
无人共我 2021-01-06 09:20

I have a question which is related to this question that was asked by @mnish about a year ago.

Please have a look at his question and code. He implements a ServiceCo

2条回答
  •  耶瑟儿~
    2021-01-06 10:11

    Here you are, my example .. will make you clear about that LOL

    // My MyServiceInterface.aidl
    package com.mad.exam;
    
    interface MyServiceInterface {
         int getNumber();
    }
    
    //MyService 
    public class MyService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
            return mBinder;
        }
    
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Toast.makeText(this, "Service Destroyed ", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
            Toast.makeText(this, "Service Started ", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            // TODO Auto-generated method stub
            return super.onUnbind(intent);
        }
    
        private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
            public int getNumber() {
                return new Random().nextInt(100);
            }
        };
    }
    
    //My Activity 
    public class ServiceDemo extends Activity implements OnClickListener {
        MyServiceInterface mService;
        ServiceConnection mConnection;
        Button retreive;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.service);
            retreive = (Button) findViewById(R.id.retreive);
            retreive.setOnClickListener(this);
    
            mConnection = new ServiceConnection() {
    
                public void onServiceDisconnected(ComponentName name) {
                    // TODO Auto-generated method stub
                }
    
                public void onServiceConnected(ComponentName name, IBinder service) {
                    // TODO Auto-generated method stub
                    mService = MyServiceInterface.Stub.asInterface(service);
    
                    try {
                        int i;
                        i = mService.getNumber();
                        Toast.makeText(ServiceDemo.this, "The service value is: " + String.valueOf(i), Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
        }
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("My Tag", "Clicked");
            Button btn = (Button) v;
            Intent callService = new Intent(this, MyService.class);
            bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            Intent callService = new Intent(this, MyService.class);
            bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
        }
    }
    

提交回复
热议问题