Android的共享preferences不节能

Android的共享preferences不节能

本文介绍了Android的共享preferences不节能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我访问我的AppWidgetProvider子类中,共享preferences如下:

I'm accessing SharedPreferences inside my AppWidgetProvider subclass as follow:

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d( TAG, "WidgetProvider.onReceive " + intent.getAction() );
        Toast.makeText( context, "WidgetProvider.onReceive " + intent.getAction(), Toast.LENGTH_LONG ); // TOAST NOT WORKING

        Bundle pExtra = intent.getExtras();
        if ( pExtra != null ) {
            SharedPreferences pref = context.getApplicationContext().getSharedPreferences("widget_pref", Context.MODE_PRIVATE );
            int iWidgetId = intent.getExtras().getInt("widgetId");
            int iCount = pref.getInt("widgetCount", 0);
                    // HERE IS THE PROBLEM, iCount will always start from 0!
            for ( int i = 0; i < 10; i++ ) {
                iCount = pref.getInt("widgetCount", 0);
                iCount++;
                pref.edit().putInt("widgetCount", iCount);
                boolean bSaved = pref.edit().commit(); // return TRUE
                bSaved = pref.contains("widgetCount"); // return FALSE
                Log.w(TAG, "WidgetProvider.onReceive: " + iWidgetId + " > " + iCount + " result: " + bSaved);
            }

                    // non-relevant code
            RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
            remoteViews.setTextViewText( R.id.hellotextview, "ID: " + iWidgetId + " > " + iCount );
            Intent clickIntent = new Intent( context, WidgetProvider.class );
            clickIntent.setAction("WIDGET_PROVIDER_CLICKED");
            clickIntent.putExtra("widgetId", iWidgetId);
            PendingIntent clickPendingIntent = PendingIntent.getBroadcast( context, 0, clickIntent, 0 );
            remoteViews.setOnClickPendingIntent( R.id.hellolayout, clickPendingIntent );
//          AppWidgetManager.getInstance(context).updateAppWidget( iWidgetId, remoteViews );
//          ComponentName name = new ComponentName(context, WidgetProvider.class);
            ComponentName name = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
            AppWidgetManager.getInstance(context).updateAppWidget(name, remoteViews);
        }
    }

不过,因为你可以从评论看,即使我循环和节约ICOUNT将始终从默认值重新开始。在包含()甚至退货,即使我只是犯假的。我试着让共享preferences不同的方式(直接使用上下文,或者context.getApplicationContext()以及不同Context.MODE_XXX)

However, as you can see from the comment, even though I am looping and saving the iCount will always start from default value again. The contains() even return false even though I just commit. I've tried different way of getting the SharedPreferences (using the context directly, or context.getApplicationContext() as well as different Context.MODE_XXX)

在此先感谢!

推荐答案

您正在创建一个新的共享preferences.Editor 通过调用对象的时候 pref.edit()进入for循环之前高速缓存中的共享preferences.Editor 对象,并重新使用该实例。

You are creating a new SharedPreferences.Editor object every time by calling pref.edit(). Cache the SharedPreferences.Editor object before entering the for loop and re-use that instance.

这篇关于Android的共享preferences不节能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:38