【问题标题】:How execute certain function on clicking on the widget?单击小部件时如何执行某些功能?
【发布时间】:2018-11-01 21:42:21
【问题描述】:

我想在单击时更改一个布尔变量值和小部件的背景? 这是小部件 xml 文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f15"
android:padding="@dimen/widget_margin"
android:id="@+id/appwidget_layout">


<TextView
    android:id="@+id/appwidget_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="8dp"
    android:textColor="#ffffff"
    android:textSize="24sp"
    android:textStyle="bold|italic" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/appwidget_button"/>

这里是小部件 java 活动

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

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_app_widget_provider);

        Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);


        PendingIntent buttonPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.appwidget_button, buttonPendingIntent);


        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

public boolean buttonState = false;

@Override
public void onReceive(Context context, Intent intent) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_app_widget_provider);

    if (intent.getAction()!= null) {

        Bundle extras = intent.getExtras();

        if (buttonState) {
            buttonState = false;
            views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.GREEN);
            views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.GREEN);
            Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
        } else {
            buttonState = true;
            views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.RED);
            views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.RED);
            Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
        }


        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context, ExampleAppWidgetProvider.class),
                views);
    }
}

}

代码运行时,onRecieve方法中的部分没有被执行。我想知道如何接收来自发送意图的同一个小部件的点击,以便我可以修改它。
提前致谢

【问题讨论】:

    标签: android widget android-widget android-appwidget


    【解决方案1】:

    在您的Widget 类之上,创建一个静态变量,这将是您的onClick

    private static final String MyOnClick = "myOnClickTag";
    

    定义一个辅助方法来自动创建每个PendingIntent

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
    

    将此onClick 标记设置为您的视图,如下所示:

    remoteViews.setOnClickPendingIntent(R.id.button, 
    getPendingSelfIntent(context, MyOnClick));
    

    在您的 Widget 类中创建一个 onReceive 方法并在其中设置此 onClick 事件:

    public void onReceive(Context context, Intent intent) {
    
        if (MyOnClick.equals(intent.getAction())){
    
        Bundle extras = intent.getExtras();
    
        if (buttonState) {
            buttonState = false;
            views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.GREEN);
            views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.GREEN);
            Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
        } else {
            buttonState = true;
            views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.RED);
            views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.RED);
            Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
        }
    
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context, ExampleAppWidgetProvider.class),
                views);
        }
    };
    

    每当您设置标签的视图被按下时,onReceive 就会捕捉到它,并将执行与我们日常的标准 onClick 事件相同的操作

    至于你的onUpdate

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)    
    {
        for (int appWidgetId : appWidgetIds) {
    
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_det);
        thisWidget = new ComponentName(context, MyWidgetProvider.class);    
        remoteViews.setOnClickPendingIntent(R.id.widget_button_stayarm, getPendingSelfIntent(context, MyOnClick1));
        remoteViews.setOnClickPendingIntent(R.id.widget_button_awayarm, getPendingSelfIntent(context, MyOnClick2));
        remoteViews.setOnClickPendingIntent(R.id.widget_button_dissarm, getPendingSelfIntent(context, MyOnClick3));
        remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "gps cords");
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
     }
     }
    

    【讨论】:

    • 非常感谢您的帮助,但它对我不起作用。我将在一个新的新项目中尝试您的代码,可能我的应用程序有问题。
    • 我用这段代码做了一个新项目,但不幸的是它也不起作用。
    • 我在onUpdate方法中添加了toast消息,toast没有出现。这意味着该方法甚至没有被调用。
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2021-09-07
    相关资源
    最近更新 更多