gpt4 book ai didi

android - 为什么不调用 onStart?

转载 作者:行者123 更新时间:2023-11-30 04:28:54 25 4
gpt4 key购买 nike

我正在尝试实现一个非常简单的服务示例。用户通过 EditText 输入值并单击计算按钮。 Calculate 按钮触发一个服务,该服务执行一些计算并将结果发送回另一个 EditText 框。如果我使用没有绑定(bind)的简单服务,结果会在执行计算之前显示,所以我想使用绑定(bind)服务。但在我的例子中,控件只是在 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();
}

@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>

最佳答案

关于android - 为什么不调用 onStart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8063826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com