我试着禁用振动显示通知时。
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);
It is 仍在振动。我测试安卓8设备。
我也尝试过
notificationChannel.setVibrationPattern(null);
仍然不起作用。
I have
<uses-permission android:name="android.permission.VIBRATE" />
无论我如何定义振动模式, like:
new long[]{1000L, 500L, 300L, 1000L};
振动与我的设置不符。仅发生默认的“两短”振动。
如果可以的话请帮忙,提前致谢。
E D I T:
正如 Avijit Karmakar 提到的,我已经添加了
notificationChannel.enableVibration(false);
完整代码 now:
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(Android 8.0)上测试
有人可以尝试这段代码并帮助我得到结果吗?