0
因此,最近几个小时我试图制作一个合适的Android小部件,目前为止没有成功。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();
}