2011-11-09 46 views
1

我想實現一個非常簡單的服務示例。 用戶通過EditText輸入值並單擊計算按鈕。計算按鈕觸發一個服務,該服務執行一些計算並將結果發送回另一個EditText框。 如果我使用一個沒有綁定的簡單服務,結果會在執行計算之前顯示出來,所以我想使用綁定服務。但在我的情況下,控制只停在onBind調用,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(); 
} 
} 
<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

你需要調用Context.startService(),以便使用在onStart(): http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent

+0

我指的是這個例如:http ://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html,並沒有提及任何有關Context.startService()的內容。請你指點一個能更好地解釋這個概念的例子嗎?謝謝。 – user988639

+0

通常情況下,如果您調用服務上的方法(即使來自多個Activity)並且想在每個Activity解除綁定後結束它,您都將使用綁定服務。 startService會請求您實現onStart()和onStop() - 因此,如果明確地要這樣做,您可以使用startService。 – Tim

+0

也提及此文檔:http://androidapps.org.ua/androidintro_gettingtoknow_service.html onBind 如果客戶端需要與服務的持久連接,則它可以調用Context.bindService方法。這將創建該服務,如果它沒有運行,並調用onCreate但不onStart。相反,onBind方法是根據客戶端的意圖調用的,它返回一個IBind對象,客戶端可以使用該對象進一步調用服務。服務讓客戶同時啓動客戶端和客戶端,這是非常正常的。 – user988639

相關問題