实时配置桌面上的AppWidget UI Layout(RemoteViews.addViews)(待修正)

实时配置桌面上的AppWidget UI Layout(RemoteViews.addViews)

新建一个继承AppWidgetProvider类的AppWidgetUILayoutActivity

首先定义一个布局文件(用于桌面显示的布局):appwidgetuilayout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="horizontal" >

 

    <Button

        android:id="@+id/appwidgetuilayout_btn01"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/add01" />

 

 

    <Button

        android:id="@+id/appwidgetuilayout_btn02"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/delete" />

 

</LinearLayout>

而后是建立一个AppwidgetProvider.xml文件

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

    android:initialLayout="@layout/appwidgetuilayout"

    android:minHeight="50dp"

    android:minWidth="100dp" >

 

</appwidget-provider>

接下来就是在AndroidManifest.xml文件中配置。

   <receiver android:name="lyx.feng.second.AppWidgetUILayoutActivity" >

            <intent-filter>

                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

            </intent-filter>

 

            <meta-data

                android:name="android.appwidget.provider"

                android:resource="@xml/appwidgetuilayout_provider" >

            </meta-data>

        </receiver>

而后就可以在桌上添加这appwidgetuilayout定义的2个Button按钮。

只是此时的按钮还没有任何的效果,要实现操作还需要AppwidgetProviderActivity的设置。

package lyx.feng.second;

 

import lyx.feng.simpletextdemo.R;

import android.app.PendingIntent;

import android.appwidget.AppWidgetManager;

import android.appwidget.AppWidgetProvider;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.widget.RemoteViews;

import android.widget.Toast;

 

public class AppWidgetUILayoutActivity extends AppWidgetProvider {

    String addButton_ActionName = "addButton";

    String deleteButton_ActionName = "deleteButton";

 

    @Override

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,

           int[] appWidgetIds) {

       Toast.makeText(context, "onUpdate()", Toast.LENGTH_SHORT).show();

 

       RemoteViews remoteViews = new RemoteViews("lyx.feng.simpletextdemo",

              R.layout.appwidgetuilayout);

 

       // 设置用来新增LayoutIntent

       Intent addIntent = new Intent(context, AppWidgetUILayoutActivity.class);

       addIntent.setAction(addButton_ActionName);

       PendingIntent addPendingIntent = PendingIntent.getBroadcast(context, 0,

               addIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.appwidgetuilayout_btn01,

              addPendingIntent);

       // 设置用来删除LayoutIntent

       Intent deleteIntent = new Intent(context,

              AppWidgetUILayoutActivity.class);

       deleteIntent.setAction(deleteButton_ActionName);

       PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context,

              0, deleteIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.appwidgetuilayout_btn02,

              deletePendingIntent);

       // 更新Appwidget

       appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

 

    }

 

    @Override

    public void onReceive(Context context, Intent intent) {

       Toast.makeText(context, "onReceiver", Toast.LENGTH_SHORT).show();

 

       if (intent.getAction().equals(addButton_ActionName)) {

           // add按钮操作

           RemoteViews remoteViews = new RemoteViews(

                  "lyx.feng.simpletextdemo", R.layout.appwidgetuilayout);

           remoteViews.removeAllViews(R.id.appwidgetuilayout_layoutID);

           RemoteViews subViews = new RemoteViews("lyx.feng.simpletextdemo",

                  R.layout.appwidgetuilayout_imageview);

           remoteViews.setTextViewText(R.id.appwidgetuilayout_btn01, "添加");

 

           remoteViews.addView(R.id.appwidgetuilayout_layoutID, subViews);

           ComponentName componentName = new ComponentName(context,

                  AppWidgetUILayoutActivity.class);

           AppWidgetManager manager = AppWidgetManager.getInstance(context);

           manager.updateAppWidget(componentName, remoteViews);

       } else if (intent.getAction().equals(deleteButton_ActionName)) {

           // Delete按钮

           RemoteViews remoteViews = new RemoteViews(

                  "lyx.feng.simpletextdemo", R.layout.appwidgetuilayout);

           remoteViews.setTextViewText(R.id.appwidgetuilayout_btn02, "删除");

           remoteViews.removeAllViews(R.id.appwidgetuilayout_layoutID);

           ComponentName componentName = new ComponentName(context,

                  AppWidgetUILayoutActivity.class);

           AppWidgetManager manager = AppWidgetManager.getInstance(context);

           manager.updateAppWidget(componentName, remoteViews);

       }

       super.onReceive(context, intent);

    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值