0

我正在尝试将我的复选框首选项与我的通知联系起来,以便当用户单击任何复选框首选项时,通知按指定工作。我希望“输入”仅在单击“警报输入”首选项时才起作用。退出和振动也是如此。

             public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String key = LocationManager.KEY_PROXIMITY_ENTERING;

    Boolean entering = intent.getBooleanExtra(key, false);

    if (entering){
        Log.d(getClass().getSimpleName(), "entering");
    }
    else{
        Log.d(getClass().getSimpleName(), "exiting");
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String alertName = prefs.getString("name_preference", "");
    String alertMessage = prefs.getString("message_preference", "");
    //boolean enteringRegion = prefs.getBoolean("alarm_entering", true);
    //boolean exitRegion = prefs.getBoolean("alarm_exiting", false);
    //boolean vibrate = prefs.getBoolean("vibrate_preference", true);

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);

    Notification notification = createNotification();
    notification.setLatestEventInfo(context, alertName, alertMessage, pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, notification);
}

private Notification createNotification(){
    Notification notification = new Notification();

    notification.icon = R.drawable.icon_notification;
    notification.when = System.currentTimeMillis();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.defaults |= Notification.DEFAULT_LIGHTS;


    long[] vibrate = new long[] {2000, 2000, 2000, 2000, 2000};
    notification.vibrate = vibrate;

    Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    notification.sound = ringURI;

    notification.ledARGB = Color.BLUE;
    notification.ledOnMS = 1500;
    notification.ledOffMS = 1500;
    notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;

    return notification;
4

1 回答 1

0

我会这样尝试:

    myPrefListner = new OnSharedPreferenceChangeListener()
    {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) 
        {
            //check if alarm key is set as wanted and if so launch notification
        }
    };

但我不太清楚你所说的“进入”是什么意思?

于 2012-09-03T06:40:37.887 回答