0

I am trying to establish a connection between my phone and bluetooth card within service in android, But i have a problem with service. Service does an auto restart after half an hour. My goal is to keep my service running as long as possible.

public void onCreate() {
Log.d("PrinterService", "Service started");   

  super.onCreate();
 }

@Override
public IBinder onBind(Intent intent) {

 return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("PrinterService", "Onstart Command");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {

    if(bluetoothProvider != null)
        bluetoothProvider = null;

        bluetoothProvider = new BluetoothProvider(getApplicationContext());
        bluetoothProvider.startUpdates();
}

return Service.START_STICKY ;
}

@Override
public void onDestroy() {
super.onDestroy();
}
user2838231
  • 71
  • 1
  • 6

1 Answers1

0

You should create a sticky service.

You can do this by returning START_STICKY in onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("LocalService", "Received start id " + startId + ": " + intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

Read also about application:persistent which is "Whether or not the application should remain running at all times". This is more troublesome - System will try not to kill your app which will effect others in the system, you should be careful using it.

refer following link: https://stackoverflow.com/a/15038770/3110609

Community
  • 1
  • 1
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40