Android小部件 - 需要处理小部件上的两个按钮点击事件...

时间:2016-02-20 11:58:49

标签: android android-widget

我在android小部件工作,是的,有很多例子,但不幸的是我无法给出确切的结果..

因为我有两个图像按钮,需要在点击时更改图片

我的班级WidgetProvider

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {

 int widgetId = appWidgetIds[i];


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

        Intent intent = new Intent(context, WidgetProvider.class);
  remoteViews.setOnClickPendingIntent(R.id.bluetooth, buildButtonPendingIntent(context));

        remoteViews.setOnClickPendingIntent(R.id.wifi, buildButtonPendingIntent1(context));
   pushWidgetUpdate(context, remoteViews);

public static PendingIntent buildButtonPendingIntent(Context context) {
    Intent intent = new Intent();
    intent.setAction("pl.looksok.intent.action.CHANGE_PICTURE");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

  public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
    ComponentName myWidget = new ComponentName(context, SimpleWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(myWidget, remoteViews);
}

我的接收器类:

public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
        updateWidgetPictureAndButtonListener(context);
    }
}

private void updateWidgetPictureAndButtonListener(Context context) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.simple_widget);

    remoteViews.setImageViewResource(R.id.bluetooth, getImageToSet());

    //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
    remoteViews.setOnClickPendingIntent(R.id.bluetooth, SimpleWidgetProvider.buildButtonPendingIntent(context));

    remoteViews.setImageViewResource(R.id.wifi, getWifi(context));
    remoteViews.setOnClickPendingIntent(R.id.wifi, SimpleWidgetProvider.buildButtonPendingIntent(context));
 SimpleWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}

     private int getWifi( Context context) {

    WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if(wifi.isWifiEnabled()){
        wifi.setWifiEnabled(false);
        return R.mipmap.ic_wifioff;
    } else{
        wifi.setWifiEnabled(true);
        return R.mipmap.ic_wifi;
    }

}

  private int getImageToSet() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (isEnabled) {
        bluetoothAdapter.disable();
        return R.mipmap.ic_batoff;
    } else {
        bluetoothAdapter.enable();
        return R.mipmap.ic_bt;

    }

}

我有一个例子和这个代码,如果我点击图像和onclick方法都在调用 任何人都可以帮我完成这个

0 个答案:

没有答案