【问题标题】:Android - update widget through widget configuration activityAndroid - 通过小部件配置活动更新小部件
【发布时间】:2016-01-15 17:55:26
【问题描述】:

所以,在过去的几个小时里,我一直在尝试制作一个合适的 Android 小部件,但到目前为止没有成功。

问题是我的 Intent 没有得到更新。

所以基本上,我有一个配置活动,当创建小部件时,它会按预期工作。

我还想做一个设置按钮,允许用户打开配置活动并更改一些设置。

特别是我希望允许用户通过小部件打开一些 pdf 文件,当它在创建小部件后立即设置时很好,但是当它通过设置按钮更改时,只有小部件的 UI(TextView 显示哪个文件将被打开),但打开文件的意图没有改变。

我正在调试代码,文件被正确选择(这就是 TextView 正确更新的原因)意图应该被正确设置,但它仍然会打开一开始选择的文件。

这是我的代码的一部分:

    Intent intent = getIntent(); //intent to get the widget ID
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);


        RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.wi_widget_layout); //widget layout
        views.setTextViewText(R.id.button_plan, fileCleanName); //setting text of the TextView showing pdf's name, works just fine

        File pdfFile = StorageModel.getFileForName(fileName, this); //method to get the file, while debugging, proper file is taken
        Intent openFile = StorageModel.openFile(pdfFile, this); // Intent method, returns intent to open pdf application for the given file

        openFile.putExtra("Random", Math.random() * 1000);
        //I've read somewhere Android is caching intents, so I tried it but didn't help

        PendingIntent pendingFile = PendingIntent.getActivity(this, 0, openFile, 0);

        views.setOnClickPendingIntent(R.id.button_plan, pdfFile);

        Intent settings = new Intent(this, WidgetConfigure.class).setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 
        //remaking intent for the settings button
       settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
        PendingIntent settingsPending = PendingIntent.getActivity(this, 0, settings, 0);
        views.setOnClickPendingIntent(R.id.button_settings, settingsPending);

        appWidgetManager.updateAppWidget(mAppWidgetId, views);

        setResult(RESULT_OK);
        finish();

    }

【问题讨论】:

    标签: android android-intent configuration widget settings


    【解决方案1】:

    如果有人碰巧遇到类似问题,显然这部分代码没问题,问题出在返回打开文件意图的方法上,尽管它很简单。 打开我的pdf文件的意图是通过调用

         createChooser 
    

    我的最终意图的方法,在返回正常意图之后,一切都在正常工作。我猜 Android 不喜欢给一个选择:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-03
      • 2017-05-07
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多