Android Notification 通知栏

private void showNotification() {
        // 消息通知栏
        // 定义NotificationManager
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        // 定义通知栏展现的内容信息
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "我的通知栏标题";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        // 定义下拉通知栏时要展现的内容信息
        Context context = getApplicationContext();
        CharSequence contentTitle = "我的通知栏标展开标题";
        CharSequence contentText = "我的通知栏展开详细内容";
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        // 用mNotificationManager的notify方法通知用户生成标题栏消息通知
        mNotificationManager.notify(1, notification);
    }
private void showNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pd = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

        // 自定义下拉视图,比如下载软件时,显示的进度条。
        Notification notification = new Notification();
        notification.icon = R.drawable.ic_launcher;
        notification.tickerText = "Custom!";

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notify_view);
        contentView.setImageViewResource(R.id.imageView_icon, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.textView_title, "湖北新闻广播");
        contentView.setTextViewText(R.id.textView_msg, "播放中");
        notification.contentView = contentView;

        // 使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法
        // 但是必须定义 contentIntent
        notification.contentIntent = pd;
        nm.notify(3, notification);
        // nm.cancel(id);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/imageView_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp" />

    <RelativeLayout
        android:id="@+id/relativeLayout_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/imageView_icon" >

        <TextView
            android:id="@+id/textView_title"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:textColor="#FFF" />

        <TextView
            android:id="@+id/textView_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView_title" />
    </RelativeLayout>

    <ImageButton
        android:id="@+id/button_notification_play"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@color/transparent"
        android:src="@drawable/play" />

</RelativeLayout>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。