为什么在onStart不叫

为什么在onStart不叫

本文介绍了为什么在onStart不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个非常简单的服务的例子。
用户通过输入的EditText的价值和点击计算按钮。计算按钮将触发执行一些计算,并将结果发送回另一个盒子的EditText服务。
如果我用一个简单的服务没有绑定,进行计算前的结果获取显示,所以我想用一个绑定的服务。但在我的情况下,控制仅仅停留在onBind呼叫,并在onStart没有得到执行。控制并流向的onCreate虽然。谁能帮我找到我要去的地方错了吗?

公共类SimpleService延伸服务{
        私人最终的IBinder mBinder =新LocalBinder();

  @覆盖
    公众的IBinder onBind(意向意图){
        // TODO自动生成方法存根
        的System.out.println(服务:OnBind);
         返回mBinder;
    }    公共类LocalBinder扩展粘结剂{        SimpleService的getService(){
            的System.out.println(服务:本地粘合剂);            返回SimpleService.this;
        }
    }
  @覆盖
  公共无效的onCreate(){
        super.onCreate();
        的System.out.println(服务:在上创建......);
        Toast.makeText(这一点,服务创造了...,Toast.LENGTH_LONG).show()
        }
  @覆盖
  公共无效的onDestroy(){
    super.onDestroy();
    的System.out.println(服务:在摧毁...);    Toast.makeText(这一点,服务破坏......,Toast.LENGTH_LONG).show();
  }    @覆盖
公共无效调用onStart(意向意图,诠释startid){
  的System.out.println(服务:在的OnStart命令);
  super.onStart(意向,startid);
  中期业绩;
  串LOG_TAG =;
  INT输入2 = intent.getIntExtra(输入,-1);
  INT模式= intent.getIntExtra(模式,-1);
  字符串ASTRING = Integer.toString(模式);
  Log.v(LOG_TAG,ASTRING);
  如果(模式== 1){
         RES = cal_F(输入2);
    }其他{
         RES = cal_C(输入2);
    }  intent.putExtra(结果,RES);
     }    }

 公共类ClassExamplesServiceActivity扩展活动实现OnClickListener {@覆盖
公共无效的onClick(视图v){    输入=的Integer.parseInt(input1.getText()的toString());
    如果(v.getId()== R.id.radio_fib)
        rd_button = 0;
    否则如果(v.getId()== R.id.radio_fact)
        rd_button = 1;
    否则如果(v.getId()== R.id.button1){        意图=新意图(这一点,SimpleService.class);
        intent.putExtra(输入,输入);
        intent.putExtra(模式,rd_button);
        doBindService();
        的System.out.println(类活动+ System.currentTimeMillis的());    }    否则如果(v.getId()== R.id.stop)
    {
        stopService(意向);
    }
}私人ServiceConnection mConnection =新ServiceConnection(){    公共无效onServiceConnected(组件名的className,服务的IBinder){
        的System.out.println(\\ n的服务连接);
        mBoundService =((SimpleService.LocalBinder)服务).getService();
    }公共无效onServiceDisconnected(组件名的className){
      的System.out.println(\\ n的服务中断);
      mBoundService = NULL;
  }
};无效doBindService(){
    的System.out.println(在做绑定服务);    布尔isConnected = bindService(新意图(ClassExamplesServiceActivity.this,SimpleService.class),mConnection,Context.BIND_AUTO_CREATE);
    intent.putExtra(输入,输入);
    intent.putExtra(模式,rd_button);
    的System.out.println(\\ n isconnected =+ isConnected);
    mIsBound = TRUE;
}
无效doUnbindService(){
    如果(mIsBound){
        RES = intent.getIntExtra(结果,-1);
        result.setText(Integer.toString(RES)); //设置结果中的EditText
        //分离我们现有的连接。
        unbindService(mConnection);
        mIsBound = FALSE;
    }
}@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    doUnbindService();
}

}

 <应用
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME>
    <活动
        机器人:标签=@字符串/ APP_NAME
        机器人:名字=ClassExamplesServiceActivity。>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <服务机器人:名字=SimpleService>< /服务>
< /用途>


解决方案

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

I am trying to implement a very simple service example.The user inputs the value through EditText and clicks on Calculate Button. The Calculate button triggers a service which performs some calculations and sends the result back to another EditText box.If I use a simple service without binding, the result gets displayed before the calculation is performed, so I want to use a bound service. But in my case, the control just stops at onBind call and onStart does not get executed. The control does flow to onCreate though. Could anyone help me to find where I am going wrong?

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>
解决方案

You need to call Context.startService() in order to use onStart():http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

这篇关于为什么在onStart不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:03