Home  >  Q&A  >  body text

android - Why can't the overridden method onBind of the service be called manually in the Activity?

There is a method onBind in the service class Service in Android. Why can't we actively call this method after creating an object inherited from the Service class in MainActivity? And the access permission of the onBind method is also public. After I obtained the MyService class object in MainActivity, I found that the onBind method could not be called. What happened?

public class MyService extends Service
{

    private DownloadBinder mBinder=new DownloadBinder();

    class DownloadBinder extends Binder
    {
        public void startDownload()
        {
            Log.d("MyService","startDownload executed");
        }

        public int getProgress()
        {
            Log.d("MyService","getProgress executed");
            return 0;
        }
    }

    public MyService()
    {
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return mBinder;
    }

}
PHP中文网PHP中文网2713 days ago564

reply all(2)I'll reply

  • 阿神

    阿神2017-05-16 13:23:50

    Although your use of Service is wrong, put aside these, if you create MyService and then call myService.onBind(null) directly, there should be no problem at the Java usage level. Not talking about DownloadBinder

    here

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:23:50

    Example:

    1. Google and you can assemble an Android phone;

    2. You assembled it yourself. If it breaks, you have to repair it yourself;

    3. Those sold by Google will be included in the brand’s warranty policy.

    The four major components are the basis for running Android applications and are maintained and managed by the system. It is not impossible to create objects of the four major components arbitrarily, but it does not make much sense.

    reply
    0
  • Cancelreply