Android点击通知栏,不启动新的activity
16lz
2021-12-04
创建通知栏
NotificationManager notificationManager(NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);//自定义布局remoteViews = new RemoteViews(getPackageName(), R.layout.view_notification_incense);mBuilder.setContent(remoteViews) .setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT)).setWhen(System.currentTimeMillis()) .setPriority(Notification.PRIORITY_DEFAULT).setOngoing(true).setSmallIcon(R.drawable.icon_lifo_top);//此图片android5.0后做成透明的mNotification = mBuilder.build();mNotification.flags = Notification.FLAG_NO_CLEAR;//显示notificationnotificationManager.notify(NOTIFICATION_ID, mNotification);//取消notificationnotificationManager.cancel(NOTIFICATION_ID);定义点击通知栏要进入的activity,若想不启动新的activity,将activity设置为 android:launchMode="singleTask"public PendingIntent getDefalutIntent(int flags) { PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(IncenseActivity.this, IncenseActivity.class), flags); return pendingIntent; }
自定义的notification布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4f0c10"> <ImageView android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/notification_icon" android:id="@+id/iv_budd" android:layout_marginLeft="13dp" /> <TextView android:id="@+id/tv_incense_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="17sp" android:textColor="@color/white" android:layout_marginTop="13dp" android:layout_marginLeft="13dp" android:layout_toRightOf="@id/iv_budd"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_incense_status" android:textColor="@color/music_progress_light" android:textSize="14sp" android:layout_marginTop="2dp" android:layout_marginLeft="13dp" android:id="@+id/tv_incense_time" android:layout_toRightOf="@id/iv_budd" />RelativeLayout>
更多相关文章
- Android(安卓)通知Notification的两种实现方法
- android 通知 Notification
- Android自定义Toast 解决关闭通知 Toast无法弹出
- Android(安卓)APP 版本更新通知代码
- android之PendingIntent使用
- Android的系统通知栏小例子
- Android(安卓)Service一前台服务(四)
- Android(安卓)UI控件详解-Notification(通知)
- 【notification】Android(安卓)中创建状态栏通知