【发布时间】:2017-03-24 15:09:40
【问题描述】:
更新 我有一个活动,我正在启动一个成功运行的服务。我正在尝试在服务中保持蓝牙连接。当应用程序处于活动状态时,蓝牙连接通过服务成功运行。但是当我关闭应用程序时,我的服务也会被杀死,蓝牙连接也会丢失。下面是我的活动代码。
http://www.vogella.com/tutorials/AndroidServices/article.html
@Override
protected void onResume() {
super.onResume();
if (bluetoothService == null) {
kolamDeviceDialog();
} else {
Log.e("MenuBluetoothService",bluetoothService.getStatus()+"");
if (!bluetoothService.getStatus()) {
kolamDeviceDialog();
}
}
}
private void kolamDeviceDialog() {
MaterialDialog.Builder builder = new MaterialDialog.Builder(MenuActivity.this)
.title("Select Bot")
.items(scanTypes)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
if (position == 0) {
KolamDevice kolamDevice = new KolamDevice();
Intent intent = new Intent(MenuActivity.this, BluetoothService.class);
intent.putExtra("Address", kolamDevice.getBigBotAddress());
intent.putExtra("Name", kolamDevice.getBigBotName());
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
} else {
KolamDevice kolamDevice = new KolamDevice();
Intent intent = new Intent(MenuActivity.this, BluetoothService.class);
intent.putExtra("Address", kolamDevice.getSmallBotAddress());
intent.putExtra("Name", kolamDevice.getSmallBotName());
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
});
builder.show();
}
@Override
protected void onPause() {
super.onPause();
// unbindService(mConnection);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder binder) {
BluetoothService.MyBinder b = (BluetoothService.MyBinder) binder;
bluetoothService = b.getService();
Toast.makeText(MenuActivity.this, "Connected", Toast.LENGTH_SHORT)
.show();
}
public void onServiceDisconnected(ComponentName className) {
bluetoothService = null;
}
};
这是我的清单文件。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".ARCameraActivity" />
<activity android:name=".RegistrationActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
<activity android:name=".ScanAndDrawActivity" />
<activity android:name=".GalleryActivity" />
<activity android:name=".PdfKolamActivity" />
<service
android:enabled="true"
android:name=".BluetoothService">
<intent-filter>
<action android:name="com.ignite.a01hw909350.kolamdemo.BluetoothService"/>
</intent-filter>
</service>
<receiver
android:name=".MyScheduleReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".MyStartServiceReceiver"/>
</application>
最后这是我的服务类。
public class BluetoothService extends Service {
private final IBinder mBinder = new MyBinder();
private SmoothBluetooth smoothBluetooth;
private static Device device;
private boolean isConnected = false;
String address, name;
private List<Integer> mBuffer = new ArrayList<>();
private List<String> mResponseBuffer = new ArrayList<>();
public BluetoothService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("BluetoothService", "Started");
Bundle extras = intent.getExtras();
if (extras != null) {
address = extras.getString("Address");
name = extras.getString("Name");
}
smoothBluetooth = new SmoothBluetooth(this);
smoothBluetooth.setListener(mListener);
if (smoothBluetooth.isBluetoothEnabled()) {
if (!smoothBluetooth.isConnected()) {
device = new Device(name, address, true);
smoothBluetooth.tryConnection();
}
}
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
BluetoothService getService() {
return BluetoothService.this;
}
}
public boolean getStatus() {
return isConnected;
}
这是我明确调用 startService() 的接收器。
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, BluetoothService.class);
context.startService(service);
}
}
请告诉我我的代码有什么问题。
【问题讨论】:
-
bindService返回什么? -
在 onServiceConnected() 方法中它说“已连接”但绑定没有发生
-
所以如果
onServiceConnected被调用,您的服务将绑定到您的活动..."but the bind is not happening"是什么意思? -
我在服务中的 onStartCommand 中保存了 Log。但它没有显示日志。怎么办?
-
onStartCommand由“已启动”服务使用(当您调用startService()方法时) - 如果您想要“绑定”服务,请阅读 developer.android.com/guide/components/bound-services.html
标签: android android-service-binding