服务onRebind()没有被调用

时间:2014-07-15 16:48:37

标签: android android-service

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

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);
}

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

1 个答案:

答案 0 :(得分:1)

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

  1. 点击“启动服务”,然后使用活动中的startService()方法启动服务。按钮。
  2. 使用活动中的bindService()方法绑定服务,方法是点击“绑定服务”'按钮。
  3. 点击'取消绑定服务'从活动中使用unbindService()方法取消绑定服务按钮。
  4. 再次点击“绑定服务”'按钮,然后活动调用onRebind()方法。
  5. 示例代码:

    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" />
    

    如果您有任何疑问,请与我联系。

    感谢。

相关问题