如何在 android 屏幕上捕捉小部件背景的点击

How to catch clicks on widget background on android screen

我有一个 4x1 可调整大小的小部件。当我点击图标时它工作正常
并打开我的应用程序,但如果我点击背景,它什么都不做
任何想法如何使我的背景可点击? 这是我的代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">
    <application>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/myWidgetXml"/>
        </receiver>
    </application>
</manifest>

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/myLayout"
    android:initialLayout="@layout/myLayout"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:resizeMode="horizontal"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen">
</appwidget-provider>

myLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin"
    android:layout_margin="0dp"
    android:gravity="center"
    android:background="@android:drawable/alert_dark_frame"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/WidgetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/widget_pic" />
    <TextView
        android:id="@+id/WidgetTextView"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:layout_marginTop="6dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
</LinearLayout>

MyWidget.java

public class MyWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds)
            updateAppWidget(context, appWidgetManager, appWidgetId);
    }

    private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout);
        Intent intent = new Intent(context, MainActivity.class);
        Uri.Builder builder = new Uri.Builder();
        intent.setData(builder.build());
        intent.setAction(Intent.ACTION_VIEW);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
        views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic);
        views.setTextViewText(R.id.WidgetTextView, "text");
        _appWidgetManager.updateAppWidget(WidgetId, views);
    }
}

只需将点击侦听器设置为 rootview,而不是 ImageButton。

现在你有:

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);

例如,将 id 指定给您的根 LinearLayout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/rootView"

并在此处更改

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);

 views.setOnClickPendingIntent(R.id.rootView, pendingIntent);

R.id.WidgetButtonR.id.rootView

在小部件中,通过在视图上应用 setOnClick() 方法,点击不会像小部件使用远程视图那样简单。

要处理点击事件,请执行以下操作:

  1. 在你的 myLayout.xml 中添加 id 到你的父布局 线性布局 android:id="@+id/lnr_widget_back"
  2. 在您的 AndroidManifest.xml 文件中:

例如,将 id 指定给您的根 LinearLayout。

<receiver android:name="com.example.MyWidget"   android:label="@string/widget_to_home" > 
<intent-filter> 
<action android:name="android.appwidget.action.APPWIDGET_UPDATE">
</action> 
</intent-filter> 
<meta-data android:name="android.appwidget.provider" android:resource="@xml/myWidgetXml"/> 
</receiver>

向您的 intent-filter 再添加一项操作,如下所示:

<action android:name="CLICK.MY_APP.WIDGET" ></action>

注意:您可以为此操作指定任何名称,但请确保它是 以自己的方式独一无二。如果它不是唯一的,你可能无法 接收点击事件/ 3. 在你的 MyWidget.java 文件中:

-> 覆盖 onReceive() 方法:

@Override public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("CLICK.MY_APP.WIDGET")) { // Your view
is clicked // Code for after click } super.onReceive(this.context,
intent); }

-> 在您的 updateAppWidget() 方法中设置将处理您的点击的待处理意图:

Intent intent = new Intent("CLICK.MY_APP.WIDGET"); PendingIntent
pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.lnr_widget_back, pendingIntent );