Android点击通知栏,不启动新的activity

创建通知栏

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;

//显示notification
notificationManager.notify(NOTIFICATION_ID, mNotification);
//取消notification
notificationManager.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布局


<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,notificati)