PendingIntent RemoteView

PendingIntent:
   PendingIntent用于描述Intent及其最终的行为.PendingIntent不是像Intent那样立即发生,而是在合适的时候才会去触发对应的 Intent.有人说这个intent不是你的ap来触发而是交给别的ap来触发。我的理解:把一个Intent包起来,在适当的时候启用该Intent。

        你可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,
例如:PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
new Intent(action), 0);  //context是要起动activity的context  
     最后一个参数设置: 通过FLAG_UPDATE_CURRENT参数的话,可以让新的Intent会更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉。

       可以通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象

        可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象

         返回的PendingIntent可以递交给别的应用程序,然后继续处理。这里的话你可以稍后才处理PendingIntent中描述的Intent及其最终行为。

       
  RemoteViews:
  RemoteViews类描述了一个View对象能够显示在其他进程中,可以融合从一个 layout资源文件实现布局。虽然该类在android.widget.RemoteViews而不是appWidget下面但在Android Widgets开发中会经常用到它,主要是可以跨进程调用(appWidget由一个服务宿主来统一运行的)。
用法:RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main1);
     rv.setTextViewText(R.id.TextView01, context.getResources().getString(R.string.load));
第一句为从远程获取了一个view对象(其实就是一个布局文件,第一个参数是布局文件所在的包名,然后第二个参数直接调用布局文件);把view(布局文件)调用过来后,实际上是想对它或他内部的控件做一些操作。例如上边第二行代码就是对view中的一个textView改变它的文本内容。
最后,我对RemoteViews的另一个方法印象比较深:RemoteViews.setOnClickPendingIntent(int viewId, PendingIntent pendingIntent);  //意思是当单击RemoteViews中的viewId指向的控件时,就会执行pendingIntent中执行的Intent,


有关appWidget的布局:
     我暂时先把它理解为一个固定的流程:
1  新建一个类,继承AppWidgetProvider,这个类就会显示在手机桌面的widget中。
2  重写里面的类,其中onReceive是AppWidgetProvider是它的父类BroadcastReciever给的。当该widiget发生变化时,调用onUpdate方法。还有好几个方法:onDelete,onEnabled,onDisabled
3  在menifest注册。因为继承了AppWidgetProvider后就是一个BroadcastReciever了,四大组件必须要注册。其中注册时需要定义meta-data元素:
<receiver android:name=".widgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider"></meta-data>
<intent-filter>
<action android:name="com.quding.action.widget.click" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
meta-data元素指定了一个布局文件(该文件放在xml文件夹中),这个布局文件也不同于普通的布局文件:
         <?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="60dp"
    android:minHeight="30dp"
    android:updatePeriodMillis="86400000"      //在桌面的显示位置
    
    android:initialLayout="@layout/main">     //这个指定了初始化的布局文件
</appwidget-provider>



有关AppWidgetManger类的使用:
// AppWidgetManger p = AppWidgetManager.getInstance(context);
/*
AppWidgetManger 类
    bindAppWidgetId(int appWidgetId, ComponentName provider)
    通过给定的ComponentName 绑定appWidgetId
    getAppWidgetIds(ComponentName provider)
    通过给定的ComponentName 获取AppWidgetId
    getAppWidgetInfo(int appWidgetId)
    通过AppWidgetId 获取 AppWidget 信息
    getInstalledProviders()
    返回一个List<AppWidgetProviderInfo>的信息
    getInstance(Context context)
    获取 AppWidgetManger 实例使用的上下文对象
    updateAppWidget(int[] appWidgetIds, RemoteViews views)
    通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
    updateAppWidget(ComponentName provider, RemoteViews views)
    通过 ComponentName 对传进来的 RemoeteView 进行修改,并重新刷新AppWidget 组件
    updateAppWidget(int appWidgetId, RemoteViews views)
    通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
* */

你可能感兴趣的:(pendingintent)