【问题标题】:Service onRebind() not being called未调用服务 onRebind()
【发布时间】:2014-07-15 16:48:36
【问题描述】:

我有一个实现以下回调方法的服务。

public bound = false;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_NOT_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
    bound = false;
    return true;
}
@Override
public void onRebind(Intent intent) {
    bound = true;

}
@Override
public void onDestroy() {
//super.onDestroy();   

}

在我的活动中,我有以下三个按钮点击的代码。

if(v.getId() == R.id.bind)
{
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);                
}
else if(v.getId() == R.id.unbind)
{
    unbindService(mConnection);
}
else if (v.getId() == R.id.rebind)
{
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

我首先绑定、取消绑定,然后调用重新绑定。在绑定和重新绑定情况下,都会调用onCreate()onBind() 服务方法。 onRebind() 永远不会被调用。我错过了什么吗?

【问题讨论】:

    标签: android android-service


    【解决方案1】:

    您可以按照以下步骤操作:

    1. 通过单击“启动服务”按钮从活动中使用 startService() 方法启动服务。
    2. 通过单击“绑定服务”按钮,使用活动中的 bindService() 方法绑定服务。
    3. 通过单击“取消绑定服务”按钮,使用活动中的 unbindService() 方法取消绑定服务。
    4. 再次单击“绑定服务”按钮,然后活动调用 onRebind() 方法。

    示例代码:

    MyService.java

    package com.example.service_lifecycle;
    
    public class MyService extends Service {
    
        @Override
        public void onCreate() {
            Toast.makeText(getApplicationContext(), "I am in on create method.", Toast.LENGTH_SHORT).show();
            super.onCreate();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Toast.makeText(getApplicationContext(), "I am in on start command method. " + mStartMode, Toast.LENGTH_SHORT).show();
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            Toast.makeText(getApplicationContext(), "I am in on bind method. " + mBinder, Toast.LENGTH_SHORT).show();
            return null;
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Toast.makeText(getApplicationContext(), "I am in on unbind method. " + mAllowRebind, Toast.LENGTH_SHORT).show();
            return true;
        }
    
        @Override
        public void onRebind(Intent intent) {
            Toast.makeText(getApplicationContext(), "I am in on rebind method.", Toast.LENGTH_SHORT).show();
            super.onRebind(intent);
        }
    
        @Override
        public void onDestroy() {
            Toast.makeText(getApplicationContext(), "I am in on destroy method.", Toast.LENGTH_SHORT).show();
            super.onDestroy();
        }
    }
    

    ServiceLifeCycleActivity.java

    package com.example.service_lifecycle;
    
    
    
    import com.example.androidpractice.R;
    
    public class ServiceLifeCycleActivity extends ActionBarActivity implements OnClickListener{
    
        private Button btnStartService;
        private Button btnStopService;
        private Button btnBindService;
        private Button btnUnbindService;
    
        private boolean isServiceBind = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_service_life_cycle);
    
            btnStartService = (Button)findViewById(R.id.btnStartService);
            btnStartService.setOnClickListener(this);
    
            btnStopService = (Button)findViewById(R.id.btnStopService);
            btnStopService.setOnClickListener(this);
    
            btnBindService = (Button)findViewById(R.id.btnBindService);
            btnBindService.setOnClickListener(this);
    
            btnUnbindService = (Button)findViewById(R.id.btnUnbindService);
            btnUnbindService.setOnClickListener(this);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.service_life_cycle, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btnStartService:          
                startService(new Intent(this, MyService.class));
                break;
    
            case R.id.btnStopService:
                stopService(new Intent(this, MyService.class));
            break;
    
            case R.id.btnBindService:
                bindService(new Intent(this, MyService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
                isServiceBind = true;
                break;
    
            case R.id.btnUnbindService:
                if(isServiceBind){
                    if(mServiceConnection != null){
                        unbindService(mServiceConnection);
                    }           
                    isServiceBind = false;
                }
                else{
                    Toast.makeText(this, "Service is not bound.", Toast.LENGTH_SHORT).show();
                }
                break;
    
            default:
                break;
            }
        }
    
        // Code to manage Service lifecycle.
            private ServiceConnection mServiceConnection = new ServiceConnection() {                    
                @Override
                public void onServiceConnected(ComponentName comName, IBinder service) {
                }
    
                @Override
                public void onServiceDisconnected(ComponentName comName) { 
    
                }       
            }; 
    }
    
    res/layout/activity_service_life_cycle.xml
    
    
        <Button
            android:id="@+id/btnStartService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Start Service" />
    
        <Button
            android:id="@+id/btnStopService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnStartService"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="19dp"
            android:text="Stop Service" />
    
        <Button
            android:id="@+id/btnBindService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/btnStopService"
            android:layout_below="@+id/btnStopService"
            android:layout_marginTop="18dp"
            android:text="Bind Service" />
    
        <Button
            android:id="@+id/btnUnbindService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/btnBindService"
            android:layout_centerVertical="true"
            android:text="Unbind Service" />
    

    如果您还有任何问题,请告诉我。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2011-09-22
      • 2018-03-03
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多