Android - 通过窗口小部件配置活动更新窗口小部件

时间:2015-10-16 21:13:13

标签: android android-intent configuration widget settings

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

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

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

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

特别是我希望允许用户通过窗口小部件打开一些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();

    }

1 个答案:

答案 0 :(得分:0)

如果有人碰巧遇到类似的问题,显然这部分代码还可以,问题在于返回意图打开文件的方法,即使它非常简单。 打开我的pdf文件的意图是通过调用

来完成的
     createChooser 

方法对我的最终意图,在返回正常意图后,一切正常。我想Android并不想做出选择:D

相关问题