【问题标题】:why public variables change in Binding Service after run App?为什么运行应用程序后绑定服务中的公共变量会发生变化?
【发布时间】:2019-04-16 07:08:52
【问题描述】:

我在Android中使用绑定服务。我用按钮启动服务和停止服务,它工作正常。故事:我按下启动服务按钮,然后服务启动,我用showint按钮增加x,x增加,然后我关闭应用程序不停止服务然后再次运行应用程序,但公共 x 变为零并再次初始化。我需要没有重新初始化公共变量的服务。我该怎么做?怎么可能绑定。

public class MainActivity extends AppCompatActivity {
    BoundService mBoundService;
    boolean mServiceBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView timestampText = (TextView) findViewById(R.id.timestamp_text);
        Button shownum = (Button) findViewById(R.id.shownum);
        Button stopService= (Button) findViewById(R.id.stop_service);
        Button start = (Button) findViewById(R.id.start);


        shownum.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mServiceBound) {
                    timestampText.setText(mBoundService.shownum());
                }
            }
        });


        stopService.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mServiceBound) {
                    unbindService(mServiceConnection);
                    mServiceBound = false;
                }
                Intent intent = new Intent(MainActivity.this,
                        BoundService.class);
                stopService(intent);

            }
        });


        start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, BoundService.class);
                startService(intent);
                bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

            }
        });

    }



    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BoundService.MyBinder myBinder = (BoundService.MyBinder) service;
            mBoundService = myBinder.getService();
            mServiceBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mServiceBound = false;
        }


    };
}

和BoundService类:

public class BoundService extends Service {
    private IBinder mBinder = new MyBinder();
    public int x;

    @Override
        public void onCreate() {
//        super.onCreate();
        Toast.makeText(this,"onCreate",Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this,"onBind",Toast.LENGTH_SHORT).show();
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent) {
        Toast.makeText(this,"onRebind",Toast.LENGTH_SHORT).show();
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Toast.makeText(this,"UUUUnbind",Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"onDestroy",Toast.LENGTH_SHORT).show();

    }

    public String shownum()
    {
        x++;
        return  String.valueOf(x);
    }

    public class MyBinder extends Binder {
        BoundService getService() {
            return BoundService.this;
        }
    }
}

【问题讨论】:

    标签: android service android-service android-service-binding


    【解决方案1】:

    从 Android 8 (Oreo) 开始,所有在应用关闭时应保持活动状态的服务应使用 startForegroundService() 启动,并应在运行时显示状态栏图标。

    如果你不这样做,当应用程序关闭时服务会被杀死。

    但是,您应该在 Logcat 中看到一些关于服务为何被终止的日志。

    【讨论】:

    • 亲爱的,我希望我的应用启动服务然后我可以关闭应用然后我运行应用并可以再次连接到服务....你能帮帮我吗?
    • StackOverflow(或其他网站)上有很多关于如何在 Android 8+ 上运行后台服务的示例。
    • 我运行我的代码,但是当我关闭 myactivity 时,BoundService 类再次运行 oncreate() ......这是我的问题
    猜你喜欢
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2023-03-26
    相关资源
    最近更新 更多