Android入门学习—Notification消息通知


最近在项目中需要使用消息通知,自己把它封装成了一个方法,需要的时候方便调用,
下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission Android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; 
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
 
             
 //设置flag位 FLAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR    该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段:
contentIntent  设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
/*******************************************分割线************************************************/
贴上源代码:
123456789101112131415161718  private void showNotification(CharSequence Title,CharSequence Text){
  //获得通知管理器
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        //构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
        Notification notification = new Notification(R.drawable.logo_notify,Title,System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失
        notification.defaults = Notification.DEFAULT_SOUND;//声音默认
        //定义下拉通知栏时要展现的内容信息 
        Context context = getApplicationContext(); 
        //点击该通知后要跳转的Activity
        Intent intent = new Intent(this,Target.class);
        BudgetSetting.budgetFlag="Setting";
        PendingIntent pendingIntent = PendingIntent.getActivity(AccountAdding.this,0,intent,0);                                                                          notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知显示的内容", pendingIntent);
        notification.setLatestEventInfo(context, Title, Text, pendingIntent);
        //用mNotificationManager的notify方法通知用户生成标题栏消息通知 
        manager.notify(1, notification);
        finish();   
 }

相关内容