簡體   English   中英

如何在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);
    }
}

只需將click listener設置為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()方法將不會像窗口小部件使用remoteviews那樣簡單。

要處理click事件,請執行以下操作:

  1. 在您的myLayout.xml中,將id添加到您的父布局,即LinearLayout 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>

如下所示,向您的意圖過濾器再添加一個操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM