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;
}
}
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
hereThe 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.