Android小部件onclick监听多个按钮

Zap*_*ica 26 android android-widget android-intent

我正在尝试为我的应用程序创建一个小部件.从我阅读一个Android开发者网站,您的onclick听众都需要有一个Intent.但是如果我只是想让我的按钮更新小部件本身的数据并且我不想开始新活动呢?

这是一些Android演示代码:

Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

我想要一个按钮,当我点击进行http网络呼叫,然后在小部件中显示结果.如果我必须使用意图,我该怎么做呢?此外,我需要能够区分单击哪些按钮.

为什么小部件使用意图而不是正常的onclick侦听器,它调用像活动这样的函数?

编辑

我的widget提供者:

public class MyWidgetProvider extends AppWidgetProvider {

private static final String MyOnClick1 = "myOnClickTag1";
private static final String MyOnClick2 = "myOnClickTag2";
private static final String MyOnClick3 = "myOnClickTag3";

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        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(widgetId, remoteViews);
    }
}

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

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

    if (MyOnClick1.equals(intent.getAction())) {
        // your onClick action is here
        Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button1");
    } else if (MyOnClick2.equals(intent.getAction())) {
        Toast.makeText(context, "Button2", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button2");
    } else if (MyOnClick3.equals(intent.getAction())) {
        Toast.makeText(context, "Button3", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button3");
    }
};
}
Run Code Online (Sandbox Code Playgroud)

我的Android清单:

<receiver
    android:name="widget.MyWidgetProvider"
    android:icon="@drawable/fsk"
    android:label="FSK Widget" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/example_appwidget_info" />
</receiver>
Run Code Online (Sandbox Code Playgroud)

can*_*ova 36

可以为窗口小部件中的视图创建onClick事件.您可以根据需要创建任意数量的onClick事件.

在Widget类的顶部,创建一个静态变量,它将是您的onClick名称标记:

private static final String MyOnClick = "myOnClickTag";
Run Code Online (Sandbox Code Playgroud)

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

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

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

    remoteViews.setOnClickPendingIntent(R.id.button, 
                      getPendingSelfIntent(context, MyOnClick));
Run Code Online (Sandbox Code Playgroud)

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

public void onReceive(Context context, Intent intent) {

    if (MyOnClick.equals(intent.getAction())){
        //your onClick action is here
    }
};
Run Code Online (Sandbox Code Playgroud)

每当按下您设置标记的视图时,onReceive将捕获该操作,并且将执行与我们日常的标准onClick事件相同的操作.

编辑:根据您的回答,您可以使用以下行替换onUpdate内容,然后重试:

    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);
Run Code Online (Sandbox Code Playgroud)

  • 很抱歉这么痛苦,但是现在已经实现了,但是当我单击小部件中的按钮时,它不会调用 onrecive 函数?我设置了断点但没有被击中?我已将我的代码粘贴到原始答案中。 (2认同)

zaP*_*yer 14

只需在onReceive方法中调用super

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);//add this line
    if (MyOnClick1.equals(intent.getAction())) {
        // your onClick action is here
        Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button1");
    } else if (MyOnClick2.equals(intent.getAction())) {
        Toast.makeText(context, "Button2", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button2");
    } else if (MyOnClick3.equals(intent.getAction())) {
        Toast.makeText(context, "Button3", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button3");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `super.onReceive(context,intent);`解决了我的问题.谢谢! (3认同)