我正在尝试让我的手机振动,如果我的平板电脑通过蓝牙与手机断开连接,我会显示一条通知。如果我通过平板电脑的蓝牙菜单断开平板电脑与手机的连接,无论是在手机屏幕打开还是关闭时(我已经让手机休眠了半个小时,然后从平板电脑的蓝牙菜单断开了连接,它都可以用)。如果我通过手机的蓝牙菜单断开手机与平板电脑的连接,它也能正常工作。如果我拿着手机离开平板电脑,并关闭手机屏幕,也是如此。
但是,如果我离开平板电脑时手机屏幕已关闭,则不会发生振动。然而,当我在几分钟后查看我的手机时,通知确实出现了,并且通知具有正确的时间戳(因此,通知不会仅在我唤醒手机时出现)。我完全被弄糊涂了。
下面是我的相关代码:
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);
}
}发布于 2015-04-24 09:21:23
不要忘记为铃声和通知启用振动设置。转到设置->声音。检查“发出振动声”。
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);https://stackoverflow.com/questions/29836801
复制相似问题