栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

切换按钮以打开通知

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

切换按钮以打开通知

您将需要将开关的值保存在某个地方(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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号