简体   繁体   English

模拟器中的Android小部件

[英]Android widget in emulator

I have an existing android app. 我有一个现有的android应用。 I added a simple widget to it using the following: 我使用以下命令向其添加了一个简单的小部件:

  • updated my manifest with a <receiver> block which provides information about my AppWidgetProvider implementation 使用<receiver>块更新了清单,该块提供了有关我的AppWidgetProvider实现的信息
  • added a new xml files in res/xml with a <appwidget-provider> element that contains the height/width/updatePeriod/initialLayout/icon/label attributes 在res / xml中使用<appwidget-provider>元素添加了一个新的xml文件,该元素包含height / width / updatePeriod / initialLayout / icon / label属性
  • added a simple default layout with an ImageView and a TextView 添加了带有ImageViewTextView的简单默认布局
  • implemented my AppWidgetProvider 实现了我的AppWidgetProvider

When I build and deploy this to the emulator my Widget doesn't show up in the list of widgets. 当我将其构建并部署到模拟器时,我的窗口小部件不会显示在窗口小部件列表中。 Am I missing some step to 'install' the widget? 我是否缺少一些“安装”小部件的步骤? Do I need to do anything special to make it appear in the emulator? 我需要做一些特殊的事情以使其出现在模拟器中吗?

EDIT: Here's what my manifest receiver looks like: 编辑:这是我的清单接收器的外观:

<receiver android:name=".MyAppWidgetProvider"
          android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/my_appwidget_info" />
</receiver>

and here's what my my_appwidget_info.xml looks like: 这是我的my_appwidget_info.xml的样子:

<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:icon="@drawable/ic_logo"
    android:label="MySampleApp"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/my_app_widget" >
</appwidget-provider>

Does your receiver tag in the manifest have the proper intent-filter and meta-data? 清单中的接收者标签是否具有正确的意图过滤器和元数据? It should look like the example in the documentation : 它看起来像文档中的示例:

<receiver android:name="MyAppWidgetProvider" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/my_appwidget_info" />
</receiver>

The receiver needs both of those pieces to be recognized as an app widget. 接收者需要将这两部分都识别为应用程序小部件。

Edit: The <receiver> tags also need to be located inside the <application> tags. 编辑: <receiver>标记也需要位于<application>标记内。

I had the same issue as well. 我也有同样的问题。 A few things to check that can stop it from showing up on different models. 要检查的几件事可以阻止它显示在不同型号上。

1)Must have a minHeight and minWidth set on AppWidget-Provider in the xml, if you remove either of those and launch in emulator, your widget will be gone. 1)必须在xml的AppWidget-Provider上设置minHeight和minWidth,如果删除其中任何一个并在模拟器中启动,则小部件都将消失。 2)Some models have issues if you don't supply a previewImage in the AppWidget-Provider 3)Some models can have issues if you install on the SD Card rather than internally, although I still prefer to install on SD Card. 2)如果您未在AppWidget-Provider中提供PreviewImage,则某些型号会出现问题。3)如果您安装在SD卡上而不是在内部安装,则有些型号会出现问题,尽管我仍然更喜欢在SD卡上安装。 4)Some models need to have an Activity with Main and Launcher defined even if you don't intend on using any Activities. 4)即使您不打算使用任何活动,某些模型也需要定义主和启动器的活动。 (You can simply make a dummy one that says 'this is a widget app only' 5)You have to have your manifest setup correctly as shown in TinJa's answer. (您可以简单地创建一个虚拟的,说“这仅是一个小部件应用程序” 5)您必须正确设置清单,如TinJa的答案所示。

I fought through this for quite a while before finally getting it to show up in all models and emulators, so be patient, make sure you aren't missing anything and set the values that need to be there. 我经过了一段时间的努力,最后才将其显示在所有模型和仿真器中,因此请耐心等待,确保您没有遗漏任何东西并设置需要存在的值。 Remember some phones cache the Widget List and may not update until you have launched your first Activity or Rebooted the phone. 请记住,某些手机会缓存“小部件列表”,并且可能不会更新,直到您启动了第一个活动或重新启动了手机。

Hope that helps. 希望能有所帮助。

Sam 山姆

If your app is installed on the SD card, all your widgets will quietly refuse to show up in the list. 如果您的应用安装在SD卡上,则所有小部件都将自动拒绝显示在列表中。 (The docs do make it clear that you can't have widgets if you're on the external storage). (文档确实明确指出,如果您位于外部存储设备上,则不能使用小部件)。

You can go to the settings in the home screen, go to Applications, and go to your app. 您可以转到主屏幕中的设置,转到“应用程序”,然后转到您的应用程序。 If it offers you to "Move to phone", then it is on the SD card. 如果它允许您“移动到手机”,则它位于SD卡上。 You can set the install location to internalOnly to prevent it from going onto the SD card. 您可以将安装位置设置为internalOnly以防止其进入SD卡。

If you do allow installation on SD card (because users will typically ask for that), they need to be clear that they can't have the widget in that case. 如果您确实允许在SD卡上进行安装(因为用户通常会要求安装),则需要清楚他们在这种情况下无法安装小部件。 If an app is on SD card and you want to use the widget, you first have to move the app back to internal storage and then reset the phone (!). 如果某个应用程序在SD卡上,并且您想使用小部件,则必须首先将其移回内部存储,然后重置手机(!)。

I had the same problem then I finally figure it out. 我遇到了同样的问题,然后我终于解决了。 Usually when you emulate an application, it has an activity to load. 通常,当您模拟应用程序时,它具有要加载的活动。 For a widget-only app, that isn't the case, but the IDE still thinks you're trying to launch an activity. 对于纯窗口小部件应用程序不是这种情况,但是IDE仍然认为您正在尝试启动活动。

To change this go to your Build Options 要更改此设置,请转到“ 构建选项”

例

then change the Launch Options to Nothing 然后将启动选项更改为“ 无”

例

I had the same problem. 我有同样的问题。 And my problem was that I have /layout/main.xml file when I delete it the widget appear! 我的问题是,删除它时我有/layout/main.xml文件,窗口小部件出现了!

It sound like @ubzack answer but I think he talk about something else [same xml file name] 听起来像@ubzack的答案,但我认为他谈论的是其他内容[相同的xml文件名]

I had the same problem. 我有同样的问题。 It turned out I accidentally had two meta-data resource xml files in two different folders, my "layout" folder and my "xml" folder. 原来,我不小心在两个不同的文件夹(“布局”文件夹和“ xml”文件夹)中有两个元数据资源xml文件。 It was finding the (empty) resource in "layout" first, and using that for its meta-data. 它首先在“布局”中找到(空)资源,然后将其用于元数据。

It was a stupid mistake, but make sure you have only one xml file of that name that's pointed to in your receiver meta-data tag. 这是一个愚蠢的错误,但是请确保您只有一个在接收器元数据标记中指向该名称的xml文件。 Also I think it should always be in the xml folder. 我也认为它应该始终在xml文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM