自定义状态栏notification布局

简介: 布局定义custom_notification.xml<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"   

布局定义custom_notification.xml

<?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="match_parent" >  
      
    <ImageView   
        android:id="@+id/image"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_alignParentLeft="true"  
        android:layout_marginRight="10dp"  
        android:contentDescription="@string/Image" />  
      
    <TextView   
        android:id="@+id/title"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/image"  
        style="@style/NotificationTitle" />  
      
    <TextView   
        android:id="@+id/text"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/image"  
        android:layout_below="@id/title"  
        style="@style/NotificationText" />  
      
</RelativeLayout>

布居中引用的样式文件styles.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
<style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
</resources>

代码

package cn.itcast.tabhost;



import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RemoteViews;

public  class FirstActivity extends Activity  {
    
    //默认点击返回键(back)会finish当前activity
    //activity栈中的所有activity都弹出后会退出当前应用
    @Override
    public void onBackPressed() {
        
        /*
         * 按照一般的逻辑,当Activity栈中有且只有一个Activity时,当按下Back键此
         * 那么下次点击此应用程序图标将从重新启动,当前不少应用程序都是采取如Home键的效果,
         * 当点击了Back键,系统返回到桌面,然后点击应用程序图标
         * 直接回到之前的Activity界面,这种效果是怎么实现的呢?通过重写按下Back键的回调函数,转成Home键的效果即可。
         */
        // 改为使用intent启动HOME桌面
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);

        // 或者,为达到此类效果,Activity实际上提供了直接的方法。
        // 将当前Activity所在的Task移到后台,同时保留activity顺序和状态。
        moveTaskToBack(true);// true表示不管是不是根都有效
    }
    
    
        /**
         * 当此Activity处于后台工作时, 在状态栏显示通知
         */
        @Override
        protected void onStop() {
            showNotification();
            super.onStop();
        }
    
    //当程序再次进入运行界面时,Activity处于onResume状态,在onResume方法中去掉状态栏的程序运行信息即可
    
        /**
         * 此Activity启动后关闭状态栏的通知
         */
        @Override
        protected void onResume() {
            // 启动后删除之前我们定义的通知
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(CUSTOM_VIEW_ID);
            super.onResume();
        }
        
        private static final int CUSTOM_VIEW_ID = 1; 
      //在状态栏显示程序通知
        private void showNotification() {
            // 创建一个NotificationManager的引用
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    
            // 定义Notification的各种属性
            Notification notification = new Notification(R.drawable.bg_normal,
                    "superGao", System.currentTimeMillis());
            
            
            
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
            contentView.setImageViewResource(R.id.image, R.drawable.i1);  
            contentView.setTextViewText(R.id.title, "自定义布局通知标题");  
            contentView.setTextViewText(R.id.text, "自定义布局通知内容"); 
            //给view设置点击事件
       /*     contentView.setOnClickPendingIntent(viewId, pendingIntent);
            */
            
            notification.contentView = contentView; 
            
            notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
            notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;//使用LED灯
            notification.defaults = Notification.DEFAULT_LIGHTS;
            notification.ledARGB = Color.BLUE;//LED灯颜色
            notification.ledOnMS = 5000;//led灯持续时间
    
            // 设置通知的事件消息
            /*
             * CharSequence contentTitle = "superGao"; // 通知栏标题
                CharSequence contentText = "love"; // 通知栏内容
            */
            Intent notificationIntent = new Intent(this, FirstActivity.class); // 点击该通知后要跳转的Activity
            PendingIntent contentItent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            notification.contentIntent=contentItent;
            
           /* notification.setLatestEventInfo(this, contentTitle, contentText,
                    contentItent);*/
    
            // 把Notification传递给NotificationManager
            notificationManager.notify(CUSTOM_VIEW_ID , notification);
    
        }
    
}


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1783056

目录
相关文章
|
3月前
|
Android开发
在mPaaS框架中,要自定义通知栏背景色
【1月更文挑战第13天】【1月更文挑战第65篇】在mPaaS框架中,要自定义通知栏背景色
27 1
|
C# Windows 容器
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
NotifyIcon控件表示系统右下角任务栏上的托盘图标,其ShowBalloonTip方法用于显示气球状提示框(Win10只有为本地Toast通知),ToolTip\oolTipText可以...
1471 0
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
|
C# 数据安全/隐私保护
【WPF】右下角弹出自定义通知样式(Notification)——简单教程
原文:【WPF】右下角弹出自定义通知样式(Notification)——简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可。
2807 0
|
Android开发
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
636 0
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
|
iOS开发
iOS 状态栏的隐藏显示与状态栏样式的设置
iOS 状态栏的隐藏显示与状态栏样式的设置
914 0
iOS 状态栏的隐藏显示与状态栏样式的设置
Qml-Dialog不能隐藏标题栏和按钮自定义
在项目中,需要弹出一个对话框来完成用户输入的功能,为了考虑界面的同一,这里需要将原生自带的标题栏隐藏掉,换成自己写的
529 0
侧滑面板(对viewGroup的自定义)
额,好吧,最近一直在做侧滑的事情,到目前为止一共是学了三种方法了,一个是直接加第三方开源框架SlidingMenu,第二给是用DrawerLayout,今天这个是用谷歌官方提供的在新的support-v4中添加了Widget  Drawer layout等侧滑效果,即ViewDragHelper,这里简单分享一下ViewDragHelper的实现方法。 ViewDragHelper
907 0