服务停止时活动关闭我用来启动服务的代码是
Intent intent2 = new Intent(getApplicationContext(), TService.class);
startService(intent2);
和停止服务的代码是
Intent intent2 = new Intent(getApplicationContext(), TService.class);
stopService(intent2);
我尝试了另一种使用绑定服务的方法,但它通过关闭活动显示了相同的行为
启动绑定服务的代码是
Intent intent2 = new Intent(getApplicationContext(), TService.class);
bindService(intent2, mServerConn, Context.BIND_AUTO_CREATE);
startService(intent2);
&安培;停止绑定服务的代码是
stopService(new Intent(getApplicationContext(), TService.class));
unbindService(mServerConn);
我的服务TService.class
public class TService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
Log.d("service", "destroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "service started");
context = getApplicationContext();
return START_NOT_STICKY;
}
}
我的活动MyActivity.java
public class MyActivity extends Activity{
Button start,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button) findViewById(R.id.start);
stop=(Button) findViewById(R.id.stop);
final ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("LOG_TAG", "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.d("LOG_TAG", "onServiceConnected");
}
};
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(getApplicationContext(), TService.class);
intent2.putExtra("value", "1");
startService(intent2);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(getApplicationContext(), TService.class);
intent2.putExtra("value", "2");
stopService(intent2);
}
});
}
private int getClicks() {
clickCount++;
return clickCount % 2 == 0 ? R.drawable.start : R.drawable.stop;
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (TService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
protected void onPause() {
}
}
我真的被困在上面,任何建议或解决方案都会被接受 提前致谢
答案 0 :(得分:0)
您需要在调用stopService之前调用unbindService。我认为这就是原因。如果服务处于绑定状态,则除非它是未绑定的,否则无法停止,这可能是您的活动被关闭的原因。
答案 1 :(得分:0)
从代码中删除startService(intent2)
以启动绑定服务。通过调用bindService(intent2, mServerConn, Context.BIND_AUTO_CREATE)
无需调用startService(intent2)
来绑定服务。并解除绑定服务只需使用unbindService(mServerConn)
答案 2 :(得分:0)
这是你的问题:
@Override
protected void onPause() {
}
你已经覆盖了onPause()
并且你没有调用super.onPause()inside it**. This will cause your app to crash when
onPause()`被调用。
你说没有堆栈跟踪,但这不正确。您可能正在过滤logcat并错过它。确保您没有过滤logcat,您将在logcat中看到错误。