1

我有两个服务(一个调用另一个服务的 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。

清单条目:

<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>
4

1 回答 1

2

首先,为什么它现在有效?

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

其次,删除那条线会破坏什么吗?

它不仅不会破坏事物,而且会使您的应用程序更快,消耗更少的系统 RAM,并且通常对用户更好。

于 2013-04-30T19:09:31.517 回答