`
龙哥IT
  • 浏览: 237776 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

通知示例2

 
阅读更多

这个主要是根据通知自定义布局开显示的

只需要在示例1中把

 PendingIntent pi = PendingIntent.getActivity(MainActivity.this,R.string.app_name,
     intent, PendingIntent.FLAG_UPDATE_CURRENT);
   n.setLatestEventInfo(MainActivity.this, "有急事", "请您马上到办公司开会", pi);
   mn.notify(R.string.app_name, n);
   

 

替换成

PendingIntent pi = PendingIntent.getActivity(MainActivity.this,R.string.app_name,
     intent, PendingIntent.FLAG_UPDATE_CURRENT);
   RemoteViews rv = new RemoteViews(MainActivity.this.getPackageName(),R.layout.notification_view);
   rv.setImageViewResource(R.id.image, R.drawable.icon);
   rv.setTextViewText(R.id.text, "嗨,美女,你好啊");
   n.contentView =rv;
   n.contentIntent=pi;
   mn.notify(R.string.app_name, n);

即可实现两者的效果,

主要区别,示例1是系统自动显示的,示例2是可以自己布局显示

然后配置notification_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="wrap_content">

 <TextView android:id="@+id/text" android:layout_width="fill_parent"
  android:layout_height="wrap_content" />

 <ImageView android:id="@+id/image" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics