仅在首次创建应用小部件时如何显示活动性? [英] How to show an actitivity only when an app widget is first created?
问题描述
我的应用程序小部件首次添加到主屏幕时,需要对其进行配置.我想在用户添加小部件后立即打开配置视图.
My app widget needs to be configured when it first added to the home screen. I want to open a configuration view right after the user adds the widget.
AppWidgetProvider
没有onCreated
事件,因此我正在显示onUpdate
事件的配置活动,并将布尔值存储在SharedConfiguration
中,该值指示已经显示了配置.有没有更简单的方法?
AppWidgetProvider
has no onCreated
event, so I'm showing the confugration activity on the onUpdate
event, and store a boolean value in SharedConfiguration
that marks that the configuration has already been shown. Is there an easier way?
推荐答案
在AppWidgetProvider XML文件中,有一个名为android:configure
In the AppWidgetProvider XML file there is an attribute called android:configure
您可以使用它来指向将应用程序拖放到主屏幕时要启动的活动.例如:
You can use this to point to an Activity to launch when the app is dropped onto the home screen. ex:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.bandsintown.WidgetSettingsActivity"
android:minWidth="250dp"
android:minHeight="110dp"
android:minResizeWidth="180dp"
android:minResizeHeight="110dp"
android:resizeMode="vertical|horizontal"
android:initialLayout="@layout/widget_layout"
android:updatePeriodMillis="10000" />
在您选择的配置活动中,用户可以做出他们想要的任何选择.您需要获取并存储小部件的ID.在onCreate
方法中,获取如下所示的ID:
In your chosen configuration activity, the user can make whatever choice they want. You'll need to grab and store the widget's id. In the onCreate
method, get the id like this:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null)
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
他们进行选择后,可使用此命令将消息发送回您的主屏幕小部件,以准备显示该消息:
When they've made their selection, use this to send a message back to your home screen widget that it is ready to be displayed:
Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, intent);
finish();
那会得到你想要的.
这篇关于仅在首次创建应用小部件时如何显示活动性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!