Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android通知首选项_Java_Android - Fatal编程技术网

Java Android通知首选项

Java Android通知首选项,java,android,Java,Android,我正在尝试将我的复选框首选项与我的通知连接起来,以便当用户单击任何复选框首选项时,通知按指定方式工作。我希望只有在单击首选项“报警输入”时,“输入”才起作用。退出和振动也一样 public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String key = LocationManager.KEY_PROXIMITY_ENTERIN

我正在尝试将我的复选框首选项与我的通知连接起来,以便当用户单击任何复选框首选项时,通知按指定方式工作。我希望只有在单击首选项“报警输入”时,“输入”才起作用。退出和振动也一样

             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;
我想这样做:

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

但我不太确定“输入”是什么意思?

当设备在通知发出之前进入某个区域时,使用输入,然后我会将输入/退出信息放在一些全局变量中,您可以在上面的
onSharedPreferenceChanged()
方法中检查。然后,您可以根据更改的复选框启动通知。