我遇到了一些没有振动器的设备的问题,如果用户从我的应用程序中的 PreferenceActivity 活动类启用应用程序中的振动器,它会崩溃。我在使用“Acer Iconia Tablet A110 Android 版本:4.1.2”等设备时遇到了这个问题
这是我正在使用的通知助手:
public void createNotification() {
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(mContext);
Boolean enableNotifications = preference.getBoolean("Enable_Notifications", true);
if(enableNotifications){
//get the notification manager
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
mNotification = new Notification();
// mNotification.defaults = Notification.DEFAULT_ALL;
mNotification.icon = mIcon;
Boolean vibrateNotifications = preference.getBoolean("Vibrate_Notifications", true);
if(vibrateNotifications){
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
}
Boolean toggleLedNotifications = preference.getBoolean("Toggle_Led_Notifications", true);
if(toggleLedNotifications){
mNotification.defaults |= Notification.DEFAULT_LIGHTS;
mNotification.ledARGB = Color.RED;
mNotification.flags = Notification.FLAG_SHOW_LIGHTS;
mNotification.ledOnMS = 1000;
mNotification.ledOffMS = 1000;
}
mNotification.tickerText = mTickerText;
if (mWhen > 0) {
mNotification.when = System.currentTimeMillis();
}
mNotification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; //Notification.FLAG_ONGOING_EVENT; make this notification appear in the 'Ongoing events' section
Boolean toggleSoundNotifications = preference.getBoolean("Toggle_Sound_Notifications", true);
if(toggleSoundNotifications){
String strRingtonePreference;
Boolean defaultAppNotificationsTone = preference.getBoolean("Default_App_Notifications_Tone", false);
// if(defaultAppNotificationsTone){
// strRingtonePreference = "android.resource://com.www.www/raw/notification_short";
// }else{
strRingtonePreference = preference.getString("List_Ringtones", "DEFAULT_SOUND");
// }
if(strRingtonePreference.equalsIgnoreCase("DEFAULT_SOUND")){
mNotification.defaults |= Notification.DEFAULT_SOUND;
}
mNotification.sound = Uri.parse(strRingtonePreference);
}
//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, mContentText, mContentIntent);
//show the notification
mNotificationManager.notify(mNotificationId, mNotification);
}
}
我应该怎么办?我应该检测设备是否有振动器吗?还是另一种方式?如何?