android的Notification折叠
16lz
2021-12-04
android 的Notification 看起来简单,但是api老是改。用的时候确实很麻烦。这里写一个折叠的Demo。
布局文件:activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="elfstton.cheerchip.com.notifitydemo.MainActivity"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Collapsed" android:onClick="collapsedNotify" />RelativeLayout>
点击事件,注意api的判断。
public void collapsedNotify(View view) { Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0); Notification.Builder builder=new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentIntent(pendingIntent); builder.setAutoCancel(true); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)); //通过RemoteViews创建自定义视图 RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.mynotification); remoteViews.setTextViewText(R.id.textView,"可折叠式"); Notification notification= null; //大于16 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification = builder.build(); }else{ //小于16 notification=builder.getNotification(); } notification.contentView=remoteViews; //通过RemoteViews创建自定义的Notification视图 RemoteViews expandedView=new RemoteViews(getPackageName(),R.layout.notification_expanded); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification.bigContentView=expandedView; } NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(0,notification); }
源码下载: https://github.com/AndriodWorld/androidnotification
更多相关文章
- 创建Android(安卓)SD卡遇到的问题
- [置顶] Android(安卓)View系统学习文章汇总
- android启动画面
- Android深入理解Context–Context使用的误区
- android 环境搭建helloworld
- 【android】Android(安卓)SDK 配置
- Android创建和使用数据库详细指南
- Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewA
- Android(安卓)4 编程入门经典