简体   繁体   English

如何制作可点击的小部件[Android]

[英]How to make clickable widget [Android]

I've created a widget, and on click an Activity has to be launched, I followed the answer to this question [Clickable widgets in android] . 我已经创建了一个小部件,然后单击必须启动的Activity,我遵循了这个问题的答案[android中可点击的小部件]

However, on click I am unable to get any response, here is the java code to my widget: 但是,单击时我无法获得任何响应,这是我的小部件的Java代码:

Widget.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);
        }
    }
}

This is the code to the xml file: 这是xml文件的代码:

widget.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>

The widget is rendered, however I am unable to get any response on click, what is possibly causing this, and how to fix? 小部件已呈现,但是单击时我无法获得任何响应,这可能是什么原因导致的,以及如何解决?

try to add AppWidgetProvider receiver in AndroidMenifest.xml like 尝试在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>

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

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