android 桌面小部件

android小部件主要是用到RemoteViews这个类,和继承AppWidgerProvider。

在res/xml下新建一个appwidget.xml这个是定义小控件的配置信息。

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget"
    android:minHeight="50dp"
    android:minWidth="100dp"
    android:configure="com.example.myremoteview.MainActivity"//
    android:widgetCategory="home_screen">

</appwidget-provider>
这个是res/layout/widget.xml布局文件,在桌面上显示的就是这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="mybutton"/>
</LinearLayout>
configure当添加小控件的时候显示的activyty
widgetCategory小控件显示的地方

initialLayout为小部件的在android桌面显示的布局

minHeight为小布局的最小高度

在AppWidgerProvider中有几个方法需要说明

onUpdate()
  当 widget 更新时被执行。

onDeleted(Context, int[])
  当 widget 被删除时被触发。

onEnabled(Context)
  当第1个 widget 的实例被创建时触发。也就是说,如果用户对同一个 widget 增加了两次(两个实例),那么onEnabled()只会在第一次增加widget时触发。

onDisabled(Context)
  当最后1个 widget 的实例被删除时触发。

onReceive(Context, Intent)
  接收到任意广播时触发,并且会在上述的方法之前被调用。

下面是继承了AppWidgetProvider的类

public class MyWidget extends AppWidgetProvider {
    private String CLICK_ACTION = "com.example.myremoteview.action";
   

    public MyWidget() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        super.onReceive(context, intent);
        //小控件点击
        if (intent.getAction() == CLICK_ACTION) {
            Toast.makeText(context, "click", Toast.LENGTH_SHORT).show();
            Log.d("test", "click");
        }
        Log.d("test", intent.getAction());

    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.d("test", "onupdata");
        //发送点击广播
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        Intent intentClick = new Intent(CLICK_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
        //下面两句一定要加上不然不会触发点击
        remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);

        }

    }

最后是清单文件的主要代码

<receiver android:name=".MyWidget">
    <meta-data
        android:resource="@xml/appwidget"
        android:name="android.appwidget.provider">

    </meta-data>
    <intent-filter>
        <action android:name="com.example.myremoteview.action" /><!--这个是点击的action-->/>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /><!--这个标识小控件的action-->/>
    </intent-filter>
</receiver>
下面是对应的小部件在桌面上占的单元格数。

单元格个数
(行 / 列)
对应的设置大小 (dp)
(minWidth / minHeight)
140dp
2110dp
3180dp
4250dp
  
n


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值