如何在接收推送通知时修复没有振动器的设备上的应用程序崩溃?

时间:2013-09-26 13:52:02

标签: java android push-notification

我遇到一些设备中没有振动器的问题,如果用户从我的应用程序中的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);
        }
    }

我该怎么办?我应该检测设备是否有振动器?或者另一种方式?怎么样?

0 个答案:

没有答案