【发布时间】: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