如果我的平板电脑通过蓝牙与手机断开连接,我试图让手机振动并显示通知。如果我通过平板电脑的蓝牙菜单将平板电脑与手机断开连接,无论是在我的手机屏幕打开还是关闭时,它都可以正常工作(我已经让我的手机睡了半个小时然后断开我的电话平板电脑的平板电脑的蓝牙菜单,它的工作原理)。如果我通过手机的蓝牙菜单断开手机与平板电脑的连接,它也可以使用。如果我在拿着手机并保持手机屏幕关闭的情况下离开平板电脑,它也能正常工作。
但是,如果我在关闭手机屏幕的情况下离开平板电脑,则不会发生振动。然而,通知确实出现了,因为我在几分钟后检查了我的手机并且通知带有正确的时间戳(因此通知不会仅在我唤醒手机时出现)。我完全不知所措。
这是我的相关代码:
public class BluetoothService extends Service {
public final BroadcastReceiver BluetoothScanner = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String trueAction = intent.getAction();
if(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(trueAction)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
dcNotify(deviceName);
Toast.makeText(context, deviceName + " has disconnected", Toast.LENGTH_LONG).show();
}
}
};
public void dcNotify(String s) {
Log.d("status", "commenced");
int notificationId = 1;
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("Device Disconnected")
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
String contentText = "Disconnected from " + s;
notificationBuilder.setContentText(contentText);
long[] pattern = { 0, 100, 500, 100, 500, 100, 500};
Notification notification = notificationBuilder.build();
notification.vibrate = pattern;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(notificationId, notification);
}
}
答案 0 :(得分:0)
不要忘记启用铃声和铃声的振动设置 通知。转到设置 - >声音。检查“声音振动”。
https://stackoverflow.com/a/13905212/972311
也不相关但使用构建器模式:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Device Disconnected")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(contentText)
.setVibrate(pattern)
.setDefaults(Notification.DEFAULT_VIBRATE);