2014-07-15 3 views
0

다음 콜백 메소드를 구현하는 서비스가 있습니다.서비스가 호출되고 있지 않습니다.

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()은 호출되지 않습니다. 내가 놓친 게 있니?

답변

1

이러한 단계를 수행 할 수 있습니다 '서비스 시작'버튼을 클릭하여 활동에서 startService() 메소드를 사용하여

  1. 시작 서비스.
  2. '서비스 바인딩'버튼을 클릭하여 액티비티에서 bindService() 메소드를 사용하여 서비스를 바인딩합니다.
  3. '서비스 바인딩 해제'버튼을 클릭하여 서비스에서 unbindService() 메소드를 사용하여 서비스의 바인딩을 해제합니다.
  4. 다시 'Bind Service'버튼을 클릭하면 액티비티가 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" /> 

알려주세요.

감사합니다.

+0

늦게 답변을 받아 죄송합니다 ... 오늘 방금 확인했습니다. – Junaid

관련 문제