2011-11-09 12 views
1

私は非常に簡単なサービスの例を実装しようとしています。 ユーザーはEditTextで値を入力し、Calculate Buttonをクリックします。 Calculateボタンは、いくつかの計算を実行し、別の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(); 
} 
} 
#あなたが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

通常、サービス上のメソッド(複数のアクティビティからでも)を呼び出し、すべてのアクティビティがバインドを解除した後に終了する場合は、バインドサービスを使用します。 startServiceはonStart()とonStop()を実装しているので、明示的にしたい場合はstartServiceを使います。 – Tim

+0

もこのドキュメントを参照してください:http://androidapps.org.ua/androidintro_gettingtoknow_service.html onBind クライアントがサービスへの永続接続を必要とする場合、Context.bindServiceメソッドを呼び出すことができます。これにより、サービスが実行されていない場合にサービスが作成され、onCreateは呼び出されますが、onStartは呼び出されません。代わりに、onBindメソッドはクライアントのインテントとともに呼び出され、クライアントがそれ以降のサービス呼び出しを行うために使用できるIBindオブジェクトを返します。サービスを開始するクライアントとクライアントが同時にバインドするようにするのは、通常のサービスです。 – user988639

関連する問題