简体   繁体   中英

How to catch clicks on widget background on android screen

I have a 4x1 resizable widget. When I click on icon it works fine
and opens my app, but if I click on the background it does nothing
Any ideas how to make my background clickable? 描述 Here's my code:

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

Just set click listener to rootview, not to ImageButton.

Now you have:

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

Give id to your root LinearLayout for example .

<?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"

And change here

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

to

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

R.id.WidgetButton to R.id.rootView

In Widget click will not work as simple by applying setOnClick() method on views as widget uses remoteviews.

For handling click event do following:

  1. In your myLayout.xml add id to your parent layout that is LinearLayout android:id="@+id/lnr_widget_back"
  2. In your AndroidManifest.xml file:

Give id to your root LinearLayout for example .

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

Add one more action to your intent-filter like below:

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

Note: You can give any name to this action but make sure that is unique in it own way. If it is not unique you may not be able to receive click event/ 3. In your MyWidget.java file:

-> overrride onReceive() method:

@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); }

-> In your updateAppWidget() method set pending intent that will handle your click:

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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