蓝牙进入服务

时间:2014-02-17 09:53:33

标签: android bluetooth

我正在尝试在Android中的服务中建立我的手机和蓝牙卡之间的连接,但我有服务问题。服务在半小时后自动重启。我的目标是让我的服务尽可能长时间运行。

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

1 个答案:

答案 0 :(得分:0)

您应该创建一个粘性服务。

您可以通过在onStartCommand中返回START_STICKY来执行此操作。

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

还阅读有关应用程序:持久性的“应用程序是否应始终保持运行”。这更麻烦 - 系统会尽量不杀死你的应用程序,这将影响系统中的其他人,你应该小心使用它。

参考以下链接: https://stackoverflow.com/a/15038770/3110609

相关问题