为何不调用onStart?

为何不调用onStart?,第1张

概述我正在尝试实现一个非常简单的服务示例.用户通过EditText输入值,然后单击CalculateButton.“计算”按钮触发服务,该服务执行一些计算并将结果发送回另一个EditText框.如果我使用没有绑定的简单服务,那么在执行计算之前将显示结果,因此我想使用绑定的服务.但就我而言,该控件仅在o

我正在尝试实现一个非常简单的服务示例.
用户通过EditText输入值,然后单击Calculate button. “计算”按钮触发服务,该服务执行一些计算并将结果发送回另一个EditText框.
如果我使用没有绑定的简单服务,那么在执行计算之前将显示结果,因此我想使用绑定的服务.但就我而言,该控件仅在onBind调用处停止,并且onStart不会执行.控件确实流向onCreate.谁能帮我找到我要去的地方?

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();    }@OverrIDepublic 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{@OverrIDepublic 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;    }}@OverrIDeprotected 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>

解决方法:

您需要调用Context.startService()才能使用onStart():
http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

总结

以上是内存溢出为你收集整理的为何不调用onStart?全部内容,希望文章能够帮你解决为何不调用onStart?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存