共享preferences没有设置boot_received广播接收机内部布尔 [英] Shared preferences not setting boolean inside boot_received broadcast receiver
本文介绍了共享preferences没有设置boot_received广播接收机内部布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建开始就启动我的服务选项。广播接收机本身的伟大工程,但是当我在选项添加它从来没有它设置为true。这里是code。
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction(); 如果(action.equals(Intent.ACTION_BOOT_COMPLETED)){
共享preferences preFS = context.getShared preferences(startatboot,0);
布尔startatboot = prefs.getBoolean(startatboot,FALSE);
如果(startatboot){
context.startService(新意图(上下文,MyService.class));
}
解决方案
我不得不改变了...
共享preferences preFS = context.getShared preferences(startatboot,0);
这...
共享preferences preFS = preferenceManager.getDefaultShared preferences(背景);公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction(); 如果(action.equals(Intent.ACTION_BOOT_COMPLETED)){
共享preferences preFS = preferenceManager.getDefaultShared preferences(背景);
布尔startatboot = prefs.getBoolean(startatboot,FALSE);
如果(startatboot){
context.startService(新意图(上下文,MyService.class));
}
I am trying to create the option to start my service on boot. The broadcast receiver works great by itself, but when I add in the option it never sets it to true. Here is the code.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
SharedPreferences prefs = context.getSharedPreferences("startatboot",0);
boolean startatboot = prefs.getBoolean("startatboot", false);
if (startatboot) {
context.startService(new Intent(context, MyService.class));
}
解决方案
I had to change this...
SharedPreferences prefs = context.getSharedPreferences("startatboot",0);
to this...
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean startatboot = prefs.getBoolean("startatboot", false);
if (startatboot) {
context.startService(new Intent(context, MyService.class));
}
这篇关于共享preferences没有设置boot_received广播接收机内部布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文