Android的更新部件的外观 [英] android update widget appearance

查看:251
本文介绍了Android的更新部件的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变,当用户点击它的一个Android小工具的视觉元素之一。这是小部件的XML

I want to change one of the visual elements of an android widget when the user clicks it. This is in the widget's xml

    <ImageView
      android:id="@+id/bg"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="false"
      android:layout_marginLeft="20dp"
      android:layout_marginTop="18dp"
      android:src="@drawable/seal" />

我想根据一个ID的改变src,所以从部件我打算送远程视窗点击监听器

 Intent bgSwitch = new Intent(widgetContext, MainActivity.class);
 bgSwitch.putExtra("jurisdiction", "bg");
 PendingIntent bgIntent = PendingIntent.getActivity(widgetContext,
 6, bgSwitch, PendingIntent.FLAG_UPDATE_CURRENT);
 remoteView.setOnClickPendingIntent(R.id.bg, bgIntent);

和透明的活动(或部件本身)我想以某种方式修改的ImageView 的来源。

and in a transparent activity (or in the widget itself) I want to somehow change the imageview 's source.

我不希望只是翻动一堆 imageviews 的知名度。

I don't want to just flip the visibility of a bunch of imageviews.

将如何实现这一目标?

推荐答案

您可以试试这个方法:

public class SwitchWidget extends AppWidgetProvider {
public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch";
public static String WIFI = "wifi";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    updateSwitch1(context, appWidgetManager, appWidgetIds[0]);
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);
    Log.d("SWitch Widget", "On Receive");



    WifiManager wifiManager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);

    RemoteViews remoteViews;

    AppWidgetManager appWidgetManager = AppWidgetManager
            .getInstance(context);

    if (SWITCH_WIDGET_UPDATE.equals(intent.getAction())) {
        Log.d("SWitch Widget", "Widget Choose");
        ComponentName thisAppWidget = new ComponentName(
                context.getPackageName(), getClass().getName());

        int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
        for (int appWidgetID : ids) {

                updateSwitch1(context, appWidgetManager, appWidgetID);
            } 

        }

    }
    if (intent.getAction().equals(WIFI)) {

            if(wifiManager.isWifiEnabled())
                wifiManager.setWifiEnabled(false);
            else
                wifiManager.setWifiEnabled(true);


        // appWidgetManager.updateAppWidget(1, remoteViews);
    }
    else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {


            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget1);
            wifiManager = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            if (wifiManager.isWifiEnabled()) {
                remoteViews.setImageViewResource(R.id.button_one,
                        R.drawable.switch_1_wifi_on);
            } else {

                remoteViews.setImageViewResource(R.id.button_one,
                        R.drawable.switch_1_wifi_off);
            }
            appWidgetManager.updateAppWidget(new ComponentName(context,
                    SwitchWidget.class), remoteViews);


    }



private void updateSwitch1(Context context,
        AppWidgetManager appWidgetManager, int appWidgetId) {
    // TODO Auto-generated method stub
    Log.d("SWitch Widget", "Switch1");
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget1);


    WifiManager wifi = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    if (wifi.isWifiEnabled())
        remoteViews.setImageViewResource(R.id.button_one, R.drawable.switch_1_wifi_on);
    else
        remoteViews
                .setImageViewResource(R.id.button_one, R.drawable.switch_1_wifi_off);

    Intent wifiIntent = new Intent(context, SwitchWidget.class);
    wifiIntent.putExtra("ID", appWidgetId);
    wifiIntent.setAction(WIFI);
    PendingIntent wifiPendingIntent = PendingIntent.getBroadcast(context,
            0, wifiIntent, 0);
    remoteViews.setOnClickPendingIntent(R.id.button_one, wifiPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

  }

和也不要忘了在你的manifest文件为补充的行动,并添加权限。

And also don't forget to add action in your manifest file and also add permissions for that.

     <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            <action android:name="wifi" />

这篇关于Android的更新部件的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆