仅在首次创建应用小部件时如何显示活动性? [英] How to show an actitivity only when an app widget is first created?

查看:88
本文介绍了仅在首次创建应用小部件时如何显示活动性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序小部件首次添加到主屏幕时,需要对其进行配置.我想在用户添加小部件后立即打开配置视图.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆