2011-11-09 4 views
1

아주 간단한 서비스 예제를 구현하려고합니다. 사용자가 EditText를 통해 값을 입력하고 계산 버튼을 클릭합니다. 계산 단추는 일부 계산을 수행하고 결과를 다른 편집 텍스트 상자로 다시 전송하는 서비스를 트리거합니다. 바인딩없이 간단한 서비스를 사용하면 계산이 수행되기 전에 결과가 표시되므로 바운드 서비스를 사용하고 싶습니다. 하지만 내 경우, 컨트롤은 onBind call에서 멈추고 onStart는 실행되지 않습니다. 그러나 컨트롤은 onCreate로 이동합니다. 누군가 내가 잘못 가고있는 곳을 찾도록 도와 줄 수 있습니까?왜 onStart가 호출되지 않습니까?

public class SimpleService extends Service { 
    private final IBinder mBinder = new LocalBinder(); 


    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     System.out.println("Service: OnBind"); 
     return mBinder; 
    } 

    public class LocalBinder extends Binder { 

     SimpleService getService() { 
      System.out.println("Service: in Local binder"); 

      return SimpleService.this; 
     } 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     System.out.println(" Service:In on create..."); 
     Toast.makeText(this,"Service created ...",   Toast.LENGTH_LONG).show() 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     System.out.println(" Service:in on destroy..."); 

     Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show(); 
    } 

@Override 
public void onStart(Intent intent, int startid) { 
    System.out.println("Service:in onstart command ..."); 
    super.onStart(intent, startid); 
    int res; 
    String LOG_TAG = ""; 
    int input2 = intent.getIntExtra("input", -1); 
    int mode = intent.getIntExtra("mode", -1); 
    String aString = Integer.toString(mode); 
    Log.v(LOG_TAG, aString); 
    if(mode == 1) { 
     res = cal_F(input2); 
    } else { 
     res = cal_C(input2); 
    } 

    intent.putExtra("result", res); 
    } 

    } 
#
public class ClassExamplesServiceActivity extends Activity implements OnClickListener{ 

@Override 
public void onClick(View v) { 

    input = Integer.parseInt(input1.getText().toString()); 
    if(v.getId() == R.id.radio_fib) 
     rd_button = 0; 
    else if(v.getId() == R.id.radio_fact) 
     rd_button = 1; 
    else if (v.getId() == R.id.button1){ 

     intent = new Intent(this, SimpleService.class); 
     intent.putExtra("input", input); 
     intent.putExtra("mode", rd_button); 
     doBindService(); 
     System.out.println("in class activity "+System.currentTimeMillis()); 

    }  

    else if(v.getId() == R.id.stop) 
    { 
     stopService(intent); 
    }  
} 

private ServiceConnection mConnection = new ServiceConnection() { 

    public void onServiceConnected(ComponentName className, IBinder service) { 
     System.out.println("\n in service connection"); 
     mBoundService = ((SimpleService.LocalBinder)service).getService(); 
    } 



public void onServiceDisconnected(ComponentName className) { 
     System.out.println("\n in service disconnected"); 
     mBoundService = null; 
    } 
}; 

void doBindService() { 
    System.out.println("in do bind service"); 

    boolean isConnected = bindService(new Intent(ClassExamplesServiceActivity.this, SimpleService.class), mConnection, Context.BIND_AUTO_CREATE); 
    intent.putExtra("input", input); 
    intent.putExtra("mode", rd_button); 
    System.out.println("\n isconnected = "+ isConnected); 
    mIsBound = true; 
} 
void doUnbindService() { 
    if (mIsBound) { 
     res = intent.getIntExtra("result", -1); 
     result.setText(Integer.toString(res));// Set the result in the EditText 
     // Detach our existing connection. 
     unbindService(mConnection); 
     mIsBound = false; 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    doUnbindService(); 
} 
} 
# 당신은 ONSTART()를 사용하기 위해 Context.startService()를 호출 할 필요가
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".ClassExamplesServiceActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name=".SimpleService"></service> 
</application> 

답변

1
+0

: HTTP를 : //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html 및 Context.startService()에 대해서는 언급하지 않았습니다. 이 개념을 더 잘 설명 할 수있는 예를 들어 주시겠습니까? 감사. – user988639

+0

일반적으로 서비스에 대한 메서드를 호출하는 경우 (둘 이상의 활동 에서조차도), 모든 활동이 바인딩을 해제 한 후에 종료하려는 경우에는 일반적으로 바인드 서비스를 사용합니다. onStart() 및 onStop()을 구현하는 startService requieres - 명시 적으로 수행하려는 경우 startService를 사용합니다. – Tim

+0

도이 문서를 참조합니다. http://androidapps.org.ua/androidintro_gettingtoknow_service.html onBind 클라이언트가 서비스에 지속적으로 연결해야하는 경우 Context.bindService 메서드를 호출 할 수 있습니다. 이렇게하면 서비스가 실행 중이 지 않을 때 서비스가 생성되고 onCreate는 호출되지만 onStart는 호출되지 않습니다. 대신 onBind 메서드는 클라이언트의 인 텐트와 함께 호출되며 클라이언트가 서비스에 대한 추가 호출을 수행하는 데 사용할 수있는 IBind 객체를 반환합니다. 서비스가 시작하는 클라이언트와 클라이언트가 동시에 바인딩하는 것은 매우 정상입니다. – user988639

관련 문제