RemoteViews在通知栏和小组件中的应用

      RemoteViews和远程Service是一样的,表示的是一个View结构,它可以在其它进程中显示,更重要的是它可以跨进程更新界面。 RemoteViews在Android中的使用场景有两种:通知栏和桌面小组件。通知栏主要是通过NotificationManager的notify方法来实现的,它除了默认效果外,还可以自定义布局。桌面小组件则是通过AppWidgetProvider来实现的,AppWidgetProvider本质上就是一个广播。接下来就看一下RemoteViews在通知栏和桌面小组件的应用。
一:RemoteViews在通知栏上的应用
      通知栏中的布局有两种,一种是系统默认的,还有一种就是自定义的布局
1:系统默认的布局
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
// Notification notify1 = new Notification(R.drawable.message,
// "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());
Notification notify1 = new Notification();
notify1.icon = R.drawable.message;
notify1.tickerText = "TickerText:您有新短消息,请注意查收!";
notify1.when = System.currentTimeMillis();
notify1.setLatestEventInfo(this, "Notification Title",
"This is the notification message", pendingIntent);
notify1.number = 1;
notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示
manager.notify(1, notify1);

2:自定义通知布局


PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
// Notification notify1 = new Notification(R.drawable.message,
// "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());
Notification notify1 = new Notification();
notify1.icon = R.drawable.message;
notify1.tickerText = "TickerText:您有新短消息,请注意查收!";
notify1.when = System.currentTimeMillis();
notify1.setLatestEventInfo(this, "Notification Title",
"This is the notification message", pendingIntent);
notify1.number = 1;
notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。

RemoteViews remoteviews = new RemoteViews(getPackageName(),R.layout.layout_nitification);
remoteviews.setTextviewText(R.id.msg,"这是一个标题");
remoteviews.setImageViewResource(R.id.icon,R.drawable,icon);
PendingIntent openActivity2 = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity2.class), PendingIntent.FLAG_UPDATE_CURRENT);
remoteviews.setOnClickPendingIntent(R.id.acitvity2,openActivity2);
PendingIntent.contentView = remoteviews;
PendingIntent.contentIntent = PendingIntent;
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示
manager.notify(2, notify1);

RemoteViews的使用很简单,只要提供当前应用的包名和布局文件的资源id即可创建一个RemoteViews对象,这里需要说明的是更新RemoteViews和更新View有很大不同。更新RemoteViews时不能直接访问里面的View,必须通过其所提供的一系列方法来更新View.比如设置Textview文本,就要采用remoteviews.setTextviewText(R.id.msg,"这是一个标题");另外如果要给控件添加单击事件,则要使用PendingIntent并通过setOnclickPendingIntent方法来实现,比如remoteviews.setOnClickPendingIntent(R.id.acitvity2,openActivity2)
;

二:RemoteViews在桌面小组件上的应用
      AppWidgetProvider是Android中提供的用于实现桌面小组件的类,其本质是一个广,所以在实际的应用中把它当成一个广播就行了。我们首先先介绍一下桌面小组键的开发步骤。
1:定义小组件界面
       在res/layout/下新建一个XML文件,命名为widget_provider.xml

2:定义小部件配置信息
       在res/xml/下新建一个appwidget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="80dp"
android:minHeight="32dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/widget_provider"
android:configure="com.demo.widget.MyWidgetConfiguration" >
initialLayout 就是指小组件使用的初始化布局,minHeight,minWidth定义小工具的最小尺寸,UpdatePeriodMillis定义小组件自动更新周期,单位是毫秒

</appwidget-provider>
3:定义小组件的实现类
      这个类要继承AppWidgetProvider,里面提供了以下函数:
onReceive(Context, Intent)
onUpdate(Context , AppWidgetManager, int[] appWidgetIds)
onEnabled(Context) onDeleted(Context, int[] appWidgetIds)
onDisabled(Context) 可通过重写以上函数来监听Widget状态的变化并进行相应的处理。 这里创建一个MyAppWidgetProvider继承AppWidgetProvider: Java代码
public class MyWidgetProvider extends AppWidgetProvider {
static final String TAG = "widget";
/** * 更新 */
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
Log.i(TAG, "onUpdate"); }
/** * 第一个Widget组件启动时触发 */
public void onEnabled(Context context){
Log.i(TAG, "onEnabled"); }
/** * 最后一个Widget组件关闭时触发 */
public void onDisabled(Context context){
Log.i(TAG, "onDisabled"); }
/** * 任一Widget组件被删除时触发
*/
public void onDeleted(Context context, int[] appWidgetIds){
Log.i(TAG, "onDeleted"); }
/** * 以上函数触发前会先触发该函数,一般不需要重写 */
public void onReceive(Context context, Intent intent){
Log.i(TAG, "onReceive");
super.onReceive(context, intent); }
}

     由于无法获取到RemoteViews创建的界面中的元素,对于Widget中组件的操作只能通过RemoteViews所提供的有限的函数进行,常用的有:
setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)
setProgressBar(int viewId, int max, int progress, boolean indeterminate)
setTextViewText(int viewId, CharSequence text)
setViewVisibility(int viewId, int visibility)


4:在AndroidManifest.xml中声明小组件
      AppWidgetProvider对应一个receiver属性: Xml代码 收藏代码
<receiver android:name="MyWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
<action android:name="com.android.lyj.main.CLICK"></action>
</intent-filter>
<meta-data
android:resource="@xml/widget_property"
android:name="android.appwidget.provider">
</meta-data>
</receiver>
     上面代码有连个Action,其中第一个用于识别小组件的单击行为,第二个Action必须存在,这是系统的规范,如果不加这个,那么receiver就不是一个桌面小组件并且也无法出现在手机的小组件列表里

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值