我创建了一个小部件,点击后必须启动一个Activity,我按照这个问题的答案 [Clickable widgets in android]。
但是,点击后我无法得到任何回复,这是我的小部件的java代码:
Widget.java
public class Widget extends AppWidgetProvider
{
private static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Intent intent = new Intent(context, HomeTeamSelector.class);
intent.setAction(YOUR_AWESOME_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent);
}
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if (intent.getAction().equals(YOUR_AWESOME_ACTION))
{
Intent x = new Intent(context,HomeTeamSelector.class);
context.startActivity(x);
}
}
}
这是xml文件的代码:
widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/widgetLayout"
android:layout_height="match_parent"
android:transitionGroup="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name_extra"
android:id="@+id/textView"
android:layout_centerInParent="true"
android:textColor="@color/colorPrimary"
android:textSize="30sp" />
</RelativeLayout>
呈现窗口小部件,但是我无法获得任何关于点击的响应,可能导致此问题的原因以及如何修复?
答案 0 :(得分:1)
尝试在AndroidMenifest.xml中添加AppWidgetProvider接收器,如
<receiver
android:name=".Widget"
android:label="@string/widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>