通过remoteViews自定义Notification通知栏的布局
来源:互联网 发布:javascript复选框全选 编辑:程序博客网 时间:2024/06/05 14:45
remoteViews,是一种远程view,通过跨进程更新自己的界面,主要用于通知栏和桌面小部件的开发过程中。
1、通知栏的实现
public class MainActivity extends Activity implements View.OnClickListener { private Button btn_one,btn_two,btn_three,btn_four; private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_one = (Button) findViewById(R.id.btn_one); btn_one.setOnClickListener(this); btn_two = (Button) findViewById(R.id.btn_two); btn_two.setOnClickListener(this); btn_three = (Button) findViewById(R.id.btn_three); btn_three.setOnClickListener(this); btn_four = (Button) findViewById(R.id.btn_four); btn_four.setOnClickListener(this); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_one://默认通知 PendingIntent pendingIntent1=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT); Notification notification1=new Notification(); notification1.icon=R.drawable.head; notification1.when=System.currentTimeMillis(); notification1.tickerText="您有新短消息,请注意查收!"; notification1.contentIntent=pendingIntent1; manager.notify(1,notification1); break; case R.id.btn_two://API11之后使用 PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT); Notification notification2=new Notification.Builder(this) .setSmallIcon(R.drawable.head)//即便自定义了图标,但有时候还是显示系统默认的,应该与手机类型有关 .setContentTitle("好消息") .setContentText("API11之后的通知") .setTicker("hello world") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent2) .setNumber(1) //在最右侧现实。这个number起到一个序列号的作用,如果多个触发多个通知(同一ID),可以指定显示哪一个。 .getNotification(); //build()是在API16及之后增加的,在API11中可以使用getNotificatin()来代替 manager.notify(1,notification2); break; case R.id.btn_three://API16之后使用 PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT); Notification notification3=new Notification.Builder(this) .setSmallIcon(R.drawable.head) .setContentTitle("好消息") .setContentText("API16之后的通知") .setTicker("hello world") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent3) .setNumber(1) .build(); manager.notify(1,notification3); break; case R.id.btn_four://自定义通知, Notification notification = new Notification(); notification.when = System.currentTimeMillis(); notification.flags = Notification.FLAG_AUTO_CANCEL; notification.tickerText = "hello world"; notification.icon = R.drawable.head;//这是个坑,如果不设置icon,通知不显示 Intent intent = new Intent(MainActivity.this, OtherActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remote_layout); remoteViews.setTextViewText(R.id.tv_title, "请假条"); remoteViews.setTextViewText(R.id.tv_content, "世界这么大,我想去看看"); remoteViews.setImageViewResource(R.id.iv_head, R.drawable.head); notification.contentIntent = pendingIntent; notification.contentView = remoteViews; manager.notify(1, notification); break; } }}
通知栏的布局remote_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/iv_head" android:layout_width="40dp" android:layout_height="40dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="3dp" android:text="内容。。。。" /> </LinearLayout></LinearLayout>
0 0
- 通过remoteViews自定义Notification通知栏的布局
- 使用RemoteViews自定义通知栏样式 Notification
- RemoteViews自定义通知栏
- Android 通知栏Notification,NotificationManager,RemoteViews使用流程,自定义通知栏样式简介
- 使用RemoteViews自定义Notification
- RemoteViews 实现简单自定义通知栏
- 使用RemoteViews自定义通知栏样式
- 自定义通知Remoteviews踩坑
- Notification通知栏在app更新时自定义布局与原生态的对比分析(自定义的布局会卡死!)
- Android的Notification的自定义布局按钮点击+点击按钮收回系统通知栏
- 通知栏发送消息Notification(可以使用自定义的布局)
- Android 通知栏发送消息Notification(可以使用自定义的布局)
- 关于Notification通知的一点愚见(自定义通知栏)
- 自定义的Notification布局
- 自定义的Notification布局
- adroid自定义notification通知栏
- RemoteViews实现自定义通知栏结合BroadCastReceiver实现动态交互
- Notification的使用(自定义Notification布局)
- java.util.AbstractSet学习笔记
- ios Protocol buffer安装
- Hibernate逆向工程(表--->对象)
- 3X3拼图的可解性
- LeetCode No.168 Excel Sheet Column Title
- 通过remoteViews自定义Notification通知栏的布局
- Spark1.6的RPC
- Ubuntu16.04 中mongodb数据库
- bitbake简单的helloworld expamle
- HashMap剖析
- C++中用const限定类的成员函数
- 可积的判定(充分条件,必要条件)
- 解决IE不支持Data.parse()的问题
- Python Socket 发送和接收十六进制数