更新appwight桌面小控件

一个小控件转换图片

private static int layoutId;

// onReceive should be something like this
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    if (bundle != null) {
        int newLayoutId = bundle.getInt("layout_id");
              layoutId = newLayoutId;
        initViews(context);
    } else {
        initViews(context);
    }
}


private void initViews(Context context) {
    RemoteViews views = null;

    // Set the initial layout   
    if (layoutId == 0) {
        Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
        Bundle layoutBundle = new Bundle();

              layoutBundle.putInt("layout_id", 1);

        PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
                layoutIntent, PendingIntent.FLAG_ONE_SHOT);

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

              views.setOnClickPendingIntent(R.id.btnNext, lPendingIntent);
    } // Else if there's some trigger to change the layout...
    else if (layoutId == 1) {
        Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
        Bundle layoutBundle = new Bundle();

        // Since I only have two layouts, I put here the id of the previous layout
        layoutBundle.putInt("layout_id", 0);

        PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
                layoutIntent, PendingIntent.FLAG_ONE_SHOT);

              views = new RemoteViews(context.getPackageName(), R.layout.layout_one);
        views.setOnClickPendingIntent(R.id.btnPrev, lPendingIntent);
    }
}

2 其实简单的实践   widget_flame_with_border_off.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
   android:gravity="center"
   android:background="@drawable/widget_frame_portrait1x1_black"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="74dip"
   android:layout_width="74dip">

   <LinearLayout
      android:id="@+id/btn_led"
      android:layout_width="52dip"
      android:layout_height="52dip"
      android:background="@drawable/appwidget_button_center"
      android:clickable="true"
      android:focusable="true"
      android:gravity="center">

      <ImageView
          android:id="@+id/img_led"
          android:layout_height="40dip"
          android:layout_gravity="center"
          android:layout_width="40dip"
          android:src="@drawable/mototorch_led_off"
          android:scaleType="fitXY" />

   </LinearLayout>
</LinearLayout>

 

widget_flame_with_border_on.xml

<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
   android:gravity="center"
   android:background="@drawable/widget_frame_portrait1x1_black"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="74dip"
   android:layout_width="74dip">

   <LinearLayout
      android:id="@+id/btn_led"
      android:layout_width="52dip"
      android:layout_height="52dip"
      android:background="@drawable/appwidget_button_center"
      android:clickable="true"
      android:focusable="true"
      android:gravity="center">

      <ImageView
          android:id="@+id/img_led"
          android:layout_height="40dip"
          android:layout_gravity="center"
          android:layout_width="40dip"
          android:src="@drawable/mototorch_led_on"
          android:scaleType="fitXY" />

   </LinearLayout>
</LinearLayout>

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
     RemoteViews views = null;

     int layoutID = 0;

     if (ledIsOn){
         layoutID = R.layout.widget_flame_with_border_on;
     }
     else {
         layoutID = R.layout.widget_flame_with_border_off;
     }

     // the layoutID that is passed in the constructor of the
     //   RemoteViews object is the layout that will be loaded
     //   when the widget is updated.
     views = new RemoteViews(context.getPackageName(), layoutID);

     for (int i = 0; i < appWidgetIds.length; i++) {
          appWidgetManager.updateAppWidget(appWidgetIds[i], views);
     }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值