当显示通知时,我尝试禁用vibration。
Func:
public static Notification buildNotifForUploaderService(Context ctx, String title, String message) {
Notification notification;
NotificationCompat.Builder notificationBuilder;
//If device is Android 8+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//setting pattern to disable vibrating
notificationChannel.setVibrationPattern(new long[]{0L});
notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
} else {
notificationBuilder = new NotificationCompat.Builder(ctx);
notificationBuilder.setVibrate(new long[]{0L});
}
notificationBuilder
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_backup_black_24dp);
notification = notificationBuilder.build();
return notification;
}
我在活动的onCreate()上调用它,如下所示:
Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
这是仍然振动的。我在Android8设备上进行了测试。我也试过
notificationChannel.setVibrationPattern(null);
仍然不起作用。
我有过
<uses-permission android:name="android.permission.VIBRATE" />
不管我如何定义振动模式,如下所示:
new long[]{1000L, 500L, 300L, 1000L};
振动与我的设置不符。Onyl默认的“两短”振动发生。
如果可以的话请帮忙,提前谢谢。
E D I T:
正如Avijit Karmakar提到的,我已经添加了
notificationChannel.enableVibration(false);
完整代码现在:
public class MainActivity extends AppCompatActivity {
final static String CHANNEL_ID = "MY_CHANNEL_ID";
final static String CHANNEL_NAME = "MY_CHANNEL_NAME";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification;
NotificationCompat.Builder mBuilder;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//Disabling vibration!
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
} else {
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setVibrate(new long[]{0L});
}
mBuilder.setContentTitle("title")
.setContentText("message")
.setSmallIcon(R.drawable.ic_android_black_24dp);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mBuilder.setLargeIcon(bm);
notification = mBuilder.build();
notificationManager.notify(1, notification);
}
}
它还在震动。
我在小米A1 (安卓8.0)上测试过
有没有人可以试一下这段代码,并帮助我得出结果?
https://stackoverflow.com/questions/48261118
复制相似问题