我正在尝试为主屏幕创建我的第一个Widget。我已经阅读了开发者网站上的教程,梳理了网络上的教程,tipps和问题解决方案,但似乎仍然无法使其工作。
问题:
我可以毫无问题地运行应用程序。我还可以从菜单中选择我的应用程序的窗口小部件(长按主屏幕 - > Android-Widgets - >可用窗口小部件列表)。当我选择我的小部件时,我收到以下错误消息(作为祝酒词): “您的手机上未安装应用程序。”
这是我的源代码:
清单中的条目:
<receiver android:name=".SubscriberStateWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/subscriber_status_widget" />
</receiver>
AppWidgetInfoProvider xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="300000"
android:configure="de.xxx.SubscriberStateWidgetProvider">
</appwidget-provider>
布局-文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/subscriberStateWidgetImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/acc_app_logo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="5dp" />
<TextView
android:id="@+id/subscriberStateWidgetTextViewLable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subscriberStatusText"
android:layout_toRightOf="@+id/subscriberStateWidgetImage"
android:layout_alignTop="@+id/subscriberStateWidgetImage"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/subscriberStateWidgetTextViewInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subscriberStateWidgetImage"
android:layout_below="@+id/subscriberStateWidgetTextViewLable"
android:layout_alignBottom="@+id/subscriberStateWidgetImage"
android:layout_marginLeft="5dp"
android:text="@string/subscriberStatusUnknown"/>
</RelativeLayout>
</FrameLayout>
的AppWidgetProvider-克拉斯:
public class SubscriberStateWidgetProvider extends AppWidgetProvider{
public String SUBSCRIBER_WIDGET_UPDATE = "subscriberWidgetUpdate";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
}
}
}
我排除的可能原因:
我还尝试在应用程序运行时创建窗口小部件(启动应用程序,按下按钮进入主屏幕)以及应用程序未运行时(启动应用程序,使用后退按钮停止应用程序)并获得相同的行为两次。
LogCat或我的日志文件中没有错误或警告条目。
我真的很喜欢你可能有的任何想法,解决方案或帮助。 提前谢谢!
答案 0 :(得分:0)
最后让它起作用:-)仍然不确定,问题出在哪里......
这是我的工作源代码:
清单中的条目:
<receiver android:name="SubscriberStateWidgetProvider" >
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/subscriber_status_widget" />
</receiver>
AppWidgetInfoProvider xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/subscriber_state_widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="300000" >
</appwidget-provider>
布局-文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/myshape" >
<TextView
android:id="@+id/subscriberStateLable"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/widgetImage"
android:layout_toRightOf="@+id/widgetImage"
android:gravity="center_horizontal|center_vertical"
android:layout_margin="4dip"
android:text="@string/subscriberStatusText" >
</TextView>
<TextView
android:id="@+id/update"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/widgetImage"
android:layout_toRightOf="@+id/widgetImage"
android:gravity="center_horizontal|center_vertical"
android:layout_margin="4dip"
android:text="@string/subscriberStatusUnknown" >
</TextView>
</RelativeLayout>
的AppWidgetProvider级:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
SubscriberStateWidgetProvider.class);
String currentSubscriberState = SubscriberStatusActivity.getCurrentSubscriberStateAsString(context);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.subscriber_state_widget_layout);
Log.w("WidgetExample", currentSubscriberState);
// Set the text
remoteViews.setTextViewText(R.id.update, currentSubscriberState);
// Register an onClickListener
Intent intent = new Intent(context, SubscriberStateWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}