Android---widget组件开发

widget就是可以在桌面上添加、删除的view.
这是一个显示时间的简单组件,主要靠service更新时间,发送给widget再更新界面。

布局

首先是Widget的布局,就是我们在桌面上看到的View的布局layout_widget.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

然后在res下创建个xml文件夹新建一个widgetconfig,作为widget的配置文件:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="100dp"
    android:minHeight="40dp"
    android:initialLayout="@layout/layout_widget"
    android:updatePeriodMillis="300000"
    >
</appwidget-provider>

minWidth、minHeight:最小宽高。
initialLayout:加载的布局
updatePeriodMillis:更新时间,最小30分钟
previewImage:3.0以后引入,widget的图标,没有的话默认应用的图标

Widget类

自定义一个WidgetProvider类继承自AppWidgetProvider,这个类是BroadcastReceiver的子类,这样你的WidgetProvider类就可以接收来自AppWidgetProvider发送的广播了。

public class WidgetProvider extends AppWidgetProvider
{
    // 组件被删除
    @Override
    public void onDeleted(Context context, int[] appWidgetIds)
    {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context)
    {
        // 最后一个组件被移除
        super.onDisabled(context);
        Intent intent = new Intent(context,TimeService.class);
        context.stopService(intent);
    }

    @Override
    public void onEnabled(Context context)
    {
        //第一个组件被添加
        super.onEnabled(context);
        Intent intent = new Intent(context,TimeService.class);
        context.startService(intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds)
    {
        // 组件更新
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

}

因为可能一个组件同时存在好几个,所以,onEnabled是在第一个组件被添加的时候调用,我们在这里开服务更新时间,onDisabled在最后一个组件被删除,我们在这里关服务。onUpdate会在每次更新的时候被调用,所以最好不要进行别的操作,如果provider中设置了android:configure属性,第一次添加组件的时候onUpdate不会被调用。

Service

后台Service服务负责更新时间

public class TimeService extends Service
{
    Timer timer;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }
    @Override
    public void onCreate()
    {
        super.onCreate();
        timer = new Timer();
        timer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                updateViews();
            }
        }, 0, 1000);
    }
    public void updateViews()
    {
        String time = sdf.format(new Date());
        // 刷新widget需要remoteviews 和 appwidgetmanager
        RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.layout_widget);
        rViews.setTextViewText(R.id.tv, time);
        AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
        ComponentName provider = new ComponentName(getApplicationContext(), WidgetProvider.class);
        manager.updateAppWidget(provider, rViews);
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}

在Service中通知WidgetProvider更新界面。

AndroidManifest.xml

最后不要忘记修改清单文件:
添加Service,添加receiver(WidgetProvider实际上是一个receiver)

<receiver android:name="com.jzd.demo2.WidgetProvider">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widgetconfig"/>
        </receiver>

最后~~~

这样我们就可以在桌面上添加自定义的小组件了,不过我们还是可以看到这个应用。这个应该只要我们安装了在组件页面就能添加组件,但是这个应用是无用的,所以用下面的方式屏蔽掉这个应用:
把MainActivity中的<category android:name="android.intent.category.LAUNCHER" /> 改成<category android:name="android.intent.category.DEFAULT" />
这个这个程序也就没有了入口,自然没有图标了,但是我们依然可以使用自定义的组件
最后是效果图:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值