Bind to service from new Context for configuration changes or bind from app context?

前端 未结 4 1817
囚心锁ツ
囚心锁ツ 2021-02-02 02:38

I\'m trying to work out if bound service is appropriate for doing background work in my app. The requirements are that various application components can make web requests throu

4条回答
  •  误落风尘
    2021-02-02 02:44

    Can you not just select configurations that you would like to handle with the configChanges attribute in your manifest and do the orientation changes in the UI manually? In this case you only need to bind to the service in onCreate and then unBind in onDestroy.

    or maybe try something like this ( I have not done proper error checking):

      
    
        class MyServiceConnection implements ServiceConnection,Parcelable {
                    public static final Parcelable.Creator CREATOR
                    = new Parcelable.Creator() {
                        public MyServiceConnection createFromParcel(Parcel in) {
                            return new MyServiceConnection(in);
                        }
    
                        public MyServiceConnection[] newArray(int size) {
                            return new MyServiceConnection[size];
                        }
                    };
    
                    @Override
                    public int describeContents() {
                        return 0;
                    }
    
                    @Override
                    public void writeToParcel(Parcel dest, int flags) {
    
                    }
    
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
    
                    }
    
                    @Override
                    public void onServiceDisconnected(ComponentName name) {
    
                    }
                }
                MyServiceConnection myServiceConnection;
                boolean configChange = false;
    
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    if (savedInstanceState != null) {
                        myServiceConnection = savedInstanceState.getParcelable("serviceConnection");
                    } else {
                        myServiceConnection = new MyServiceConnection();
                    }
    
                }
                @Override
                protected void onSaveInstanceState(Bundle outState) {
                    super.onSaveInstanceState(outState);
                    if (myServiceConnection != null) {
                        outState.putParcelable("serviceConnection",myServiceConnection);
                        configChange = true;
                    }
                }
                @Override
                protected void onDestroy() {
                    super.onDestroy();
                    if (!configChange && myServiceConnection != null){
                        unbindService(myServiceConnection);
                    }
                }
            }
    
    

提交回复
热议问题