android - Why can't the overridden method onBind of the service be called manually in the Activity?
PHP中文网
PHP中文网 2017-05-16 13:21:50
0
2
606

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中文网

认证高级PHP讲师

reply all(2)
阿神

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
Ty80

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template