0

I use bind service in Android .I start Service and stop Service with button and it work correctly.The story: I press start service button so Service start then,I increase x with showint button and x increase well, then I close App without stopping service then run App again , but public x turn to zero and initial again.I need service without re-initial public variable.How can i do that? How can bind .

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;
        }


    };
}

and BoundService class:

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;
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Moh_beh
  • 251
  • 1
  • 3
  • 14

1 Answers1

0

Starting from Android 8 (Oreo) all Services that should stay alive when the App is closed should be started using startForegroundService() and should show a statusbar icon while running.

If you don't do that the Service is killed when the App is closed.

However you should see some log in Logcat regarding WHY the Service is killed.

emandt
  • 2,547
  • 2
  • 16
  • 20
  • dear , i want my app start service then i can close app then i run app and can connect to service again.... can u help me? – Moh_beh Apr 16 '19 at 13:32
  • There are many and many examples on StackOverflow (or on other websites) about how to run a background service on Android 8+. – emandt Apr 16 '19 at 14:55
  • i run my code but when i close myactivity , BoundService class run oncreate() again.......this is my problem – Moh_beh Apr 16 '19 at 16:38
  • https://stackoverflow.com/questions/55710144/how-can-start-service-several-times-while-service-is-alive this is my question with picture – Moh_beh Apr 16 '19 at 16:40