我的共享偏好不会更新,

时间:2017-06-06 19:43:27

标签: android android-intent sharedpreferences appwidgetprovider

我一直试图让这个工作暂时没有成功。我已经没有选择,希望你们能帮忙。 我想使用sharedprefernce将文本保存在编辑文本中。然后将该文本发送到我的appwidgetprovider,当单击小部件时,该文本将文本发回以进行编辑。当编辑完成并保存时,它应该将文本发送回appwidgetprovider,依此类推。

但事实并非如此。编辑文本会打开以前保存的文本,但是一旦我保存它就不会更新共享首选项。 app-widget提供程序中共享首选项的值保持不变。

这是将文本发送到appwidgetprovider的类。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(widget_handler);

    editText = (EditText) findViewById(R.id.widget_note);
    editText.requestFocus();
    final InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm2.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_widget);
    setSupportActionBar(toolbar);
    editText.setText(getIntent().getStringExtra("Name"));



  toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
    String newEntry = editText.getText().toString();
        final SharedPreferences sharedPreferences=getSharedPreferences("Key",WidgetHandler.MODE_PRIVATE);
        final SharedPreferences.Editor editor=getSharedPreferences(newEntry,WidgetHandler.MODE_PRIVATE).edit();
        sharedPreferences.edit().putString("Name",newEntry).apply();
        editor.putString("Name",newEntry);
        editor.apply();
        Intent intent3= getIntent();
        Bundle extras=intent3.getExtras();

        if (extras!= null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }


            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
            RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.new_app_widget);

            remoteViews.setTextViewText(R.id.appwidget_text, newEntry);
            appWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
           final  Intent intent = new Intent();
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            intent.putExtra("Name",newEntry);

        setResult(RESULT_OK, intent);


            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            finish();
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.addCategory(Intent.CATEGORY_HOME);
            home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(home);


    }
});

这是我的appwidgetprovider,它应该接收更新的文本。

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

       SharedPreferences sharedPreferences=context.getSharedPreferences("Key",Context.MODE_PRIVATE);
        String name=sharedPreferences.getString("Name","");
        Log.e("name",name);

        final Intent clickIntent = new Intent(context, WidgetHandler.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
        clickIntent.putExtra("Name",name);
        PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        views.setOnClickPendingIntent(R.id.appwidget_text,clickPI);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);




    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}





@Override
public void onEnabled(Context context) {

    // Instruct the widget manager to update the widget


}

@Override
public void onDisabled(Context context) {


    // Enter relevant functionality for when the last widget is disabled
}

提前致谢:)。

1 个答案:

答案 0 :(得分:0)

尝试使用editor.commit()而不是editor.apply()