您将需要将开关的值保存在某个地方(SQLite或SharedPreferences),并检查它是否为当前开关值,然后将其值保持 在选中或未选中的位置
这是创建SharedPreferences并放入值的方法:
SharedPreferences sharedPreferences = getSharedPreferences("key", 0); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("switchValue", aSwitch.isChecked()); editor.apply();这是检查SharedPreferences值并进行更改的方法:
Boolean showNotifications = editor.getBoolean("key",false); if (aSwitch.isChecked() != showNotifications) { editor.putBoolean("key",!showNotifications).apply(); }您要在其中通知通知的位置,应检查SharedPreferences值:
Boolean showNotifications = editor.getBoolean("key",false);已编辑 这是您触发通知的方式:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.your_drawable) .setStyle(new NotificationCompat.BigTextStyle().bigText("notification text")) .setContentText(notificationToNotify.getText()) .setContentTitle(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "notification title")) .setContentIntent(contentPendingIntent) .setDeleteIntent(deletePendingIntent) .setAutoCancel(true); //notify the Notification mNotifyMgr.notify(notification_id, mBuilder.build());notification_id-您可以为每个通知提供的int ID。您可以设置删除意图和内容意图(如果用户单击通知)
男人,您需要使用相同的“密钥”。像那样:
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("key", 0);SharedPreferences.Editor editor = sharedPreferences.edit();editor.putBoolean("**switchValue**", aSwitch.isChecked());editor.apply();boolean showNotifications = editor.getBoolean("**switchValue**",false);


