Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
活动在android中停止服务后关闭_Android_Android Activity_Android Service - Fatal编程技术网

活动在android中停止服务后关闭

活动在android中停止服务后关闭,android,android-activity,android-service,Android,Android Activity,Android Service,当服务停止时,活动关闭。我用来启动服务的代码是 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);
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);
&停止绑定服务的代码被删除

Intent intent2 = new Intent(getApplicationContext(), TService.class);
bindService(intent2, mServerConn, Context.BIND_AUTO_CREATE);
startService(intent2); 
stopService(new Intent(getApplicationContext(), TService.class));
unbindService(mServerConn);
我的服务类

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() {

    }


}
我真的很坚持,任何建议或解决方案都会被接受
提前感谢

在拨打stopService之前,您需要先拨打unbindService。我想这就是原因。如果服务处于绑定状态,则无法停止服务,除非它未绑定,这可能是您的活动关闭的原因。

从代码中删除startServiceintent2以启动绑定服务。通过调用bindServiceintent2、mServerConn、,Context.BIND\u AUTO\u创建无需调用startServiceintent2。而且,要解除绑定服务,只需使用UnbindServicesServerConn即可。这是您的问题:

@Override
protected void onPause() {

}
您已覆盖onPause,并且**未调用super.onPauseinside它**。这将导致在调用NonPause`时应用程序崩溃


你说没有stacktrace,但那是不对的。您可能正在筛选logcat,但没有找到它。确保您没有过滤日志猫,您将在日志猫中看到错误。

如果我的操作不起作用,请发布活动和服务的完整代码,尝试发布stacktrace…没有生成stacktrace,我还在寻找相同的服务,如果我使用bindServiceintent2,mServerConn,Context.BIND_AUTO_CREATE only但在unbindservicemserverconn上调用destroy然后绑定服务时出现严重错误..我正在使用服务绑定和解除绑定,它对我来说工作正常..因此请发布活动和服务的代码即使我使用正常的后台运行服务它也会关闭我添加的活动super.on暂停,但活动仍会关闭logcat,仅以下错误A/libc8395:致命信号11 SIGSEGV,0x00000010代码=1,线程8395