[Precautions for using the four major components - Service]

  1. Calling startService() multiple times will only call onStartCommand() repeatedly

Calling bindService(...) multiple times will only call onReBind() repeatedly. Unless the Service is already unBind, the onBind() method will not be called even if bindService() is called multiple times.

若 onBind()没有被调用则 ,ServiceConnection 的回调方法 onServiceConnected(..) 就不会被调用。

Therefore, if you need to do some important business logic in the callback method onServiceConnected(…) of ServiceConnection, you cannot expect that the onServiceConnected(…) method must be called by calling bindService().

  1. Need to pay attention to a problem:

    When the Activity exits, the Service will not stop. At this time, we can enter the Activity to rebind. At this time, the Service will call the onRebind() method, but the premise of calling the onRebind() method is the previous onUnbind() method. The execution is successful, but using super.onUnbind(intent) is unsuccessful,

    At this time, we need to manually make it return true, and Rebind() will be executed when it is bound again. Otherwise, if the specified onUnbind() that is not displayed when exiting is successful (false), then when the Activity is restarted to bind the service, the Service's onBind() method and onReBind will not be executed, but the ServiceConnection method will definitely be executed. Called back. This shows that the onBind() method in the Service is different from the onStart() method and cannot be called repeatedly.

    The source code is as follows:

      /**
         * Called when all clients have disconnected from a particular interface
         * published by the service.  The default implementation does nothing and
         * returns false.
         * 
         * @param intent The Intent that was used to bind to this service,
         * as given to {@link android.content.Context#bindService
         * Context.bindService}.  Note that any extras that were included with
         * the Intent at that point will <em>not</em> be seen here.
         * 
         * @return Return true if you would like to have the service's
         * {@link #onRebind} method later called when new clients bind to it.
         */
        public boolean onUnbind(Intent intent) {
          
          
            return false; //默认返回 FALSE ,因此如果想要 onRebind()可以被调用,则子类需要重写此方法。
        }
    
  2. It is recommended to use stopSelf(int) to ensure that the service is stopped at the most recent start request

    If the service handles multiple onStartCommand() requests at the same time, you should not stop the service after processing the first start request, it is possible that you have received a new start request (stopping the service at the end of the first request will terminate the second request) . To avoid this problem, stopSelf(int) can be used to ensure that the service stops at the most recent start request. That is, when calling stopSelf(int), pass the ID of the start request (the startId passed to onStartCommand()) that corresponds to the ID of the stop request. Then, if the service receives a new request before stopSelf(int) is called, the ID does not match and the service will not stop.
    Note: In order to avoid wasting system resources and small battery power, the application must stop its service after the work is completed. Other components can stop the service if necessary by calling stopService, even if binding is enabled for the service, it is always necessary to stop the service yourself once the service receives a call to onStartCommand.

  3. [Recommendation] Always use an explicit Intent to start or bind a Service, and do not declare an Intent Filter for the service
    to ensure the security of the application. If you really need to use implicit calls, you can set the Intent - Filter for the Service and exclude related component names from the tent, but you must use the intent.setPackage() method to set the specified package name of the Intent (only the current application is allowed The Intent of the package name can open this Service).

  4. [Recommendation] Service needs to process multiple startup requests concurrently with multiple threads. It is recommended to use IntentService to process time-consuming operations, which can avoid many complicated settings.

    Example: The onReceive method of PushMessageReceiver inherited from BroadcastReceiver in Xiaomi Push will start a Service called MessageHandleService every time a broadcast is received, and this MessageHandleService is actually an IntentService.

Guess you like

Origin blog.csdn.net/UserFrank/article/details/129204103