三、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:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章