Android开发之App Widget(二)

什么是PendingIntent?

PendingIntent对Intent进行了包装,当出现某种事件后再执行Intent。


RemoteViews的作用

1.RemoteViews对象表示了一系列的View对象

2.RemoteViews所表示的对象运行在另外的进程当中


在App Widget当中使用控件的步骤(注意:本文的例子在上篇博客的基础上进行修改

1.在example_appwidget.xml中添加要在AppWidget中显示的控件。

2.新建TargetActivity作为点击AppWidget控件后要启动的Activity。

3.在ExampleAppWidgetProvider.java中的onUpdate()方法中进行控件监听器的设置与绑定。(由于AppWidget与应用程序不在同一个进程,AppWidget当中的View运行在Home Screen进程中,所以不能用之前惯用的方法绑定监听器


example_appwidget.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/widgetButton"
        android:text="测试用按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

activity_target.xml:(此为TargetActivity的布局文件,TargetActivity类为空)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.appwidget.TargetActivity">
    
    <TextView
        android:text="测试用Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

ExampleAppWidgetProvider.java:

package com.mycompany.appwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class ExampleAppWidgetProvider extends AppWidgetProvider{

    //  在到达指定的更新时间之后或者当用户向桌面添加App Widget时调用
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            System.out.println(appWidgetIds[i]);
            //  创建一个Intent对象
            Intent intent = new Intent(context, TargetActivity.class);

            //  创建一个PendingIntent
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);

            //  为按钮绑定事件处理器
            //  第一个参数用来指定被绑定处理器的控件的ID
            //  第二个参数用来指定当时间发生时,哪个PendingIntent将会被执行
            remoteViews.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

            //  更新AppWidget
            //  第一个参数用于指定被更新的AppWidget的ID
            //  第二个参数用于要被更新的控件对象
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        System.out.println("------>onUpdate");
    }

    //  当App Widget被删除时调用
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        System.out.println("------>onDeleted");
    }

    //  当第一个App Widget的实例第一次被创建时调用
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        System.out.println("------>onEnabled");
    }

    //  当最后一个App Widget实例被删除后调用
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        System.out.println("------>onDisabled");
    }

    //  接收广播事件
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值