什么是PendingIntent?
Intent是一个意图,一个描述了想要启动一个Activity、Broadcast或是Service的意图。它主要持有的信息是它想要启动的组件(Activity、Broadcast或是Service)。
PendingIntent可以看作是对Intent的包装。供当前App之外的其他App调用。有点“被动”或是“Callback”的意思,但不是严格意义上的“被动”或是“Callback”。总之,当前App不能用它马上启动它所包裹的Intent。而是在外部App执行这个PendingIntent时,间接地、实际地调用里面的Intent。PendingIntent主要持有的信息是它所包装的Intent和当前App的Context。正由于PendingIntent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行PendingIntent里的Intent,就算在执行时当前App已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
下面来个图解:
从上图可以看出:进程A和进程B代表两个不同的应用程序。pendingIntent 就像一个包装盒,把真正的Intent进行了包裹,从而进程A就创建了一个PendingIntent。
但是进程A创建了PendingIntent后自己不使用,而是把它交给进程B
交给进程B也不是马上就执行了,而是有一定的事件去触发才能执行。
当满足某一个事件后就可以执行这个PendingIntent了,比如单击事件等……
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情
创建PendingIntent的方法
PendingIntent 通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例
创建PendingIntent对象不是New出来的,而是调用PendingIntent的三个静态方法来创建的。
getActivity 启动一个Activity
getBroadcast 发送一个广播
getService 启动一个服务
RemoteViews的作用
RemoteViews就是能在另一个进程中显示页面布局的层次。层次是在布局文件中定义,它提供了一些改变布局文件的方法定义。
1.RemoteViews对象表示一系列的View对象。
2.RemoteViews所表示的对象运行在另一进程当中。非常重要的一条
动手做一个试试
先来看下项目结构图:
首先,在layout文件夹中创建一个example_appwidget.xml布局文件,在里面创建一个按钮。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/widgetButtonId"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="Widget"/>
</LinearLayout>
有按钮当然就有监听事件,所以为Button绑定监听器
由于AppWidget和我们的应用程序运行在不同的进程当中(AppWidget的View运行在Home Screen进程中),所以之前用的绑定是行不通的。
需要用到以下方法:
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
R.id.widgetButtonId就是一个按钮
pendingIntent就是所要执行的pendingIntent对象
当单击这个按钮时就会执行PendingIntent对象所包裹的intent。
现在来做一个小实例
创建xml文件夹并创建example_appwidget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/example_appwidget"
>
</appwidget-provider>
创建layout布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/widgetButtonId"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="Widget"/>
</LinearLayout>
创建TargetActivity作为跳转的目标Activity
package sjllef.appwidget02;
import android.app.Activity;
import android.os.Bundle;
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
创建
package sjllef.appwidget02;
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 {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for(int i = 0; i < appWidgetIds.length; i++) {
System.out.println("appWidgetId--->" + 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.widgetButtonId, pendingIntent);
//更新AppWidget
//第一个参数用来指定被更新AppWidget的ID
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
看一下效果。点击前
点击后
摘自:mars