【问题标题】:android update widget appearanceandroid更新小部件外观
【发布时间】:2012-12-17 11:10:16
【问题描述】:

当用户单击它时,我想更改一个 android 小部件的视觉元素。这是在小部件的 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,所以我计划从小部件发送 remoteView 点击listener

 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 的来源。

我不想只是翻转一堆imageviews 的可见性。

这将如何实现?

【问题讨论】:

    标签: android android-widget remoteview


    【解决方案1】:

    你可以试试这个方法:

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

    并且不要忘记在清单文件中添加操作并为此添加权限。

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

    【讨论】:

    • @CQM 如果它适合你。您可以将其标记为答案。不管它是否适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多