gpt4 book ai didi

android - getDefaultSharedPreferences 不在服务之间保存

转载 作者:行者123 更新时间:2023-11-29 21:44:16 25 4
gpt4 key购买 nike

我有两个服务(调用另一个服务的 DashClock 扩展服务),第二个服务应该保存一个值,以便我可以使用该值显示在我的 DashClock 扩展中。但是,此值似乎没有传递到扩展服务中。

DashClock 扩展:

@Override
public void onCreate() {
super.onCreate();
be=this;
//numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -1);
Intent i = new Intent(this, UpdateCheckService.class);
i.putExtra(Constants.CHECK_FOR_UPDATE, true);
startService(i);
//numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", 0);

registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}

@Override
protected void onUpdateData(int reason) {
// Get preference value.
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//String name = sp.getString(PREF_NAME, "Eric");
Log.d("hiya","updating");
/*while(getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -1)==-1){
Log.d("wait","waiting here");
}*/
//numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -2);
int numUpdates = sp.getInt("numUpdates", -2);
Log.d("IMPORTANT FROM BrianExtension", "" + numUpdates);
Intent intent = new Intent();
intent.setClassName("com.cyanogenmod.updater", "com.cyanogenmod.updater.UpdatesSettings");
// Publish the extension data update.
if(numUpdates == 0){
shouldShow = false;
} else{
shouldShow = true;
}
publishUpdate(new ExtensionData()
.visible(shouldShow)
.icon(R.drawable.cid)
.status(numUpdates + " updates")
.expandedTitle(numUpdates + " Nightly CM Updates")
.expandedBody("Click here to go to CMUpdater.")
.clickIntent(intent));
}

更新检查服务:

@Override
public void onCreate() {
ucs = this;
registerReceiver(mReceiver, new IntentFilter("updatethis"));
/*prefs = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
prefs.registerOnSharedPreferenceChangeListener(mListener);*/
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
try {
int ericUpdates = getAvailableUpdates().getUpdateCount(getBaseContext());
Log.d("yes!", ""+ericUpdates);
sp.edit().putInt("numUpdates", ericUpdates).apply();
Log.d("IMPORTANT FROM UpdateCheckService", "" + sp.getInt("numUpdates", -99));
//getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putInt("numUpdates", ericUpdates).apply();
//Log.d("IMPORTANT FROM UpdateCheckService", "" + getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -99));
sendBroadcast(new Intent(ucs, BrianUpdaterReceiver.class));
//BrianExtension.be.onUpdateData(4);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
//stopSelf();
// Get the System Mod string
mSystemMod = TESTING_DOWNLOAD ? "cmtestdevice" : SysUtils.getSystemProperty(Customization.BOARD);
if (mSystemMod == null) {
Log.i(TAG, "Unable to determine System's Mod version. Updater will show all available updates");
}
}

日志显示“numUpdates”首选项中的值在 UpdateCheckService 中是正确的,但在 BrianExtension 中仍然是 -2。

list 条目:

<service android:name=".BrianExtension"
android:icon="@drawable/cid"
android:label="CyanogenMod Updates"
android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA">
<intent-filter>
<action android:name="com.google.android.apps.dashclock.Extension" />
</intent-filter>
<meta-data android:name="protocolVersion" android:value="1" />
<meta-data android:name="description"
android:value="Displays Number of New Nightly Updates Available" />
</service>

<service android:name="com.brianco.cyanogenmodupdatecheckfordashclock.UpdateCheckService"
android:process="com.brianco.cyanogenmodupdatecheckfordashclock.UpdateCheckService">
<intent-filter>
<action android:name="com.brianco.cyanogenmodupdatecheckfordashclock.IUpdateCheckService"/>
</intent-filter>
</service>

最佳答案

First, why is it now working?

因为您使用了两个独立的进程。 SharedPreferences 最初被读入,然后在进程的剩余生命周期内有效地只写。对于同时运行的两个进程,一个进程无法看到另一个进程的变化。

Second, will removing that line break anything possibly?

它不仅不会破坏东西,而且会让您的应用更快,消耗更少的系统内存,并且通常对用户来说更好。

关于android - getDefaultSharedPreferences 不在服务之间保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com