问题描述
显示通知时,我尝试禁用振动.
功能:
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()上这样称呼它:
I call this on an activity's onCreate() like this:
Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
仍在振动.我在 Android 8 设备上进行了测试.我也尝试过
It is still vibrating. I test on Android 8 device.I have also tried
notificationChannel.setVibrationPattern(null);
仍然不起作用.
我有
<uses-permission android:name="android.permission.VIBRATE" />
无论我如何定义振动模式,例如:
new long[]{1000L, 500L, 300L, 1000L};
振动与我的设置不符. Onyl默认为两个短位"发生振动.
The vibration does not correspond to my settings. Onyl the default "two short" vibration occurs.
如果可以的话,请帮助.
Please help if you can, thanks in advance.
E D I T:
正如Avijit Karmakar所述,我添加了
As Avijit Karmakar mentioned, I have added
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);
}
}
它还在振动.
我在小米Mi A1(Android 8.0)上进行了测试
It is still vibrating.
I test on Xiaomi Mi A1 (Android 8.0)
有人可以尝试使用此代码并帮助我获得结果吗?
Can anybody try this code and help me with the results?
推荐答案
类似于答案,请执行以下操作:
Like this answer, do:
mNotificationChannel.setVibrationPattern(new long[]{ 0 });
mNotificationChannel.enableVibration(true);
重要事项1 :即使我在上面设置了振动模式,但将enableVibration设置为false也会振动.因此,请将enableVibration设置为 true !
Important 1: even if I set the vibration pattern above, but set enableVibration to false, it vibrates. So, set enableVibration to true!
重要2 :像这样的另一个答案,该频道会保留其初始设置,因此卸载,然后再次安装该应用以应用更改!
Important 2: like this another answer, the channel keeps its initial settings, so uninstall and install the app again to apply the changes!
希望有帮助!
这篇关于无法在Android 8中禁用通知振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!