三、RemoteViews

RemoteViews

RemoteViews在Android中的应用场景有两种:通知栏和桌面小部件。

1.RemoteViews的应用

RemoteViews在实际的开发过程中,主要用在通知栏和桌面小部件的开发中。
通知栏主要是通过NotificationManager的notify方法来实现的,它除了默认效果,还可以自定义布局。
桌面小部件则是通过AppWidgetProvider来实现的,它本质上是一个广播。

通知栏和桌面小部件的开发过程都会用到RemoteViews,它们在更新界面的时候无法像Activity里面那样去直接更新View,这是因为二者的界面都是运行在其他的进程中,确切来说是系统的SystemServer进程。

为了跨进程更新界面,RemoteViews提供了一系列的set方法,并且这些方法只是Views全部方法的子集,另外,RemoteViews中所支持的View类型也是有限制的。

(1)RemoteViews在通知栏的应用
1)系统默认样式的通知

                   sId++;
                  Notification notification = new Notification();
                   notification. icon = R.drawable. ic_launcher;
                   notification. tickerText = "hello world";
                   notification. when = System. currentTimeMillis();
                   notification. flags = Notification. FLAG_AUTO_CANCEL;
                  Intent intent = new Intent( this, DemoActivity_2.class);
                  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                               intent, PendingIntent.FLAG_UPDATE_CURRENT );
                   notification. setLatestEventInfo(this, "chapter_5",
                               "this is notification.", pendingIntent );
                  NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE );
                   manager.notify( sId, notification);

2)自定义样式的通知

                   sId++;
                  Notification notification = new Notification();
                   notification. icon = R.drawable. ic_launcher;//在状态栏提示的图标
                   notification. tickerText = "hello world";//在状态栏提示的文字
                   notification. when = System. currentTimeMillis();
                   notification. flags = Notification. FLAG_AUTO_CANCEL;
                  Intent intent = new Intent( this, DemoActivity_1.class);
                   intent.putExtra( "sid", "" + sId);
                  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                               intent, PendingIntent.FLAG_UPDATE_CURRENT );
                  System. out.println( pendingIntent);
                  RemoteViews remoteViews = new RemoteViews(getPackageName(),
                              R.layout. layout_notification );
                   remoteViews.setTextViewText(R.id. msg, "chapter_5: " + sId);
                   remoteViews.setImageViewResource(R.id. icon, R.drawable.icon1);
                  PendingIntent openActivity2PendingIntent = PendingIntent
                              . getActivity(this, 0,
                                           new Intent( this, DemoActivity_2.class),
                                          PendingIntent. FLAG_UPDATE_CURRENT);
                   remoteViews.setOnClickPendingIntent(R.id. open_activity2,
                               openActivity2PendingIntent);
                   notification. contentView = remoteViews;
                   notification. contentIntent = pendingIntent;
                  NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE );
                   manager.notify( sId, notification);

更新RemoteViews必须通过RemoteViews所提供的一系列方法来更新view。

(2)RemoteViews在桌面小部件的应用
1)定义小部件
在res/layout下面新建一个XML文件,命名为widget.xml,名称和内容可以
自定义,看这个小部件要做成什么样子。

<?xml version="1.0" encoding= "utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id= "@+id/imageView1"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:src= "@drawable/icon1" />
</LinearLayout>

2)定义小部件配置信息
在res/xml下新建appwidget_provider_info.xml,名称随意,添加内容:

<?xml version="1.0" encoding= "utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout= "@layout/widget"
    android:minHeight="84dp"
    android:minWidth="84dp"
    android:updatePeriodMillis= "86400000" >
</appwidget-provider>

上面的参数的意思:
initialLayout是小工具初始化的布局;
minHeight和minWidth定义小工具的最小尺寸;
updatePeriodMillis定义小工具的自动更新的周期,毫秒为单位。

3)定义小部件的实现类

4)Manifest声明

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

            <intent-filter >
                <action android:name ="com.ryg.chapter_5.action.CLICK" />
                <action android:name= "android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter >
        </receiver >

两个action,第一个用于标识识别小部件的单机行为,第二个则作为小部件的标识,必须存在。

未完。。。

这篇估计不会写了

:cry:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值