您的位置:首页 > 移动开发

Android开发之App Widget(二)

2016-09-21 19:28 330 查看

什么是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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息