zoukankan      html  css  js  c++  java
  • ANDROID Notification

    Android Notification.

    主要类

    1. Notification是通知消息的载体,我们发送通知的时候需要把一些列的数据封装到Notification中,然后调用NotificationManager.notify(id,notification)来进行消息的发送。

    2. NotificationManager是通知的管理者通知的发送,取消、都是通过它来操作。

    3. NotificationCompat,可以build Notification对象

    Google官方文档参考:docs/reference/android/app/NotificationManager.html;

    docs/reference/android/app/Notification.html。

    使用场景

    例,在service,activity,broadcastRecevice,中发送系统通知,

    如图:

    clip_image001

    如何使用

    这才是我们的主题,怎么在项目中应用我们的知识点。

    1.标准的发送Notification ,例;在service,activity,broascaseRecevice中调用这个方法,就可以发送广播

    /**
         * 
         * @param context 传入context,这里可以是Service,BroadcaseRecevice,Activity.
         */
        private void showNotifi(Context context, int id) {
    
            //获取NotificationManager对象,来管理通知消息的发送与取消
            NotificationManager notifiManger = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
    
            //创建通知载体
            Notification notification = new Notification();
    
            //头上的图标
            notification.icon = R.drawable.ic_launcher;
    
            //添加振动式要添加权限,android.permission.VIBRATE;声音可以自定义格式,如果没有使用系统的默认通知声音,
            notification.defaults = Notification.DEFAULT_ALL;
    
            //自定义声音
            //要使用应用程序指定的声音,则传递一个Uri引用给sound属性。以下例子使用已保存在设备SD卡上的音频文件作为提示音:
            //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
            //在下面的例子里,音频文件从内部MediaStore类的ContentProvider中获取:
            //注意:如果defaults属性包含了“DEFAULT_SOUND”,则缺省提示音将覆盖sound 属性里指定的声音。
            //如果期望在用户响应通知或取消通知前,声音一直持续循环播放,可以把 “FLAG_INSISTENT” 加入flags属性中。
            //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
    
            //自定义闪光
            /*notification.ledARGB = 0xff00ff00; 
    
            notification.ledOnMS = 500; 
    
            notification.ledOffMS = 3000; 
    
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;*/
    
            //自定义振动
            long[] vibrate = { 0, 100, 200, 300, 400, 500, 600, 700 };
    
            notification.vibrate = vibrate;
    
            //在flags属性中增加此标志,则将通知放入通知窗口的“正在运行”(Ongoing)组中。表示应用程序正在运行——进程仍在后台运行,即使应用程序不可见(比如播放音乐或接听电话)。
            //notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    
            //“FLAG_NO_CLEAR”标志在flags属性中增加此标志,表示通知不允许被“清除通知”按钮清除。这在期望通知保持运行时特别有用。
            //notification.flags |= Notification.FLAG_NO_CLEAR;
    
            notification.tickerText = "最新消息,明天大幅度降温";
    
            //点击跳转的Intent
            PendingIntent pendingItent = PendingIntent.getActivity(context, 0,
                    new Intent(this, MainActivity.class), 0);
    
            notification.setLatestEventInfo(context, "天气通知,通知的id:" + id,
                    java.lang.System.nanoTime() + ":根据国家气象局发布的天气明天会有降温",
                    pendingItent);
    
            //id,是通知的标示,如果id,不同就会发送
            notifiManger.notify(id, notification);
        }

     2.系统给出的视图远远满足不了客服的需求,作为开发人员来讲就是要客服一切困难来满足客服,在自定义的Notification View中用到的是RemoteView远程视图,经常被用到Notification和桌面插件中,这里只贴出部分代码,会提供下载地址例:

    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
                    R.layout.noti_myself);
    notification.contentView = remoteView;
    
    //由于没有使用set...来设置点击Notification时候的跳转方向,所以我们要自己设置
    notification.contentIntent = pendingItent;

     3.在2.3之后,google团队对Notification的界面有做了比较大的跳转,具体是那些方面的调整了,我们可以参考下官方给出的文档:

    http://developer.android.com/guide/topics/ui/notifiers/notifications.html

    网友总结的一个中文版:

    http://www.cnblogs.com/tianjian/archive/2012/12/31/2840862.html

    3.1里面有的就不多说了。这个需要注意的是,我们有几个属性必须设置。

    //创建通知载体,在2.3之后不直接去new Notificationd对象 NotificationCompat.Builder
            Builder notifiBuilder = new NotificationCompat.Builder(this);
    
            //必须设置的3个属性
            notifiBuilder.setSmallIcon(R.drawable.app_window);
            notifiBuilder.setContentTitle("消息提醒");
            notifiBuilder.setContentText("您的电脑出问题了");

     3.2 如果没有设置大图标,系统会默认取应用的图标。

    notifiBuilder.setLargeIcon(BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.app_window));

    3.3TaskStackBuilder的使用,它是用来管理我们调整到那个Activity的,他可以用来产生一个标准的Activity,用户在HomeScreem点击Notification进入到一个Activity之后,按back键返回式返回到程序的主页中去、或者是直接返回到homeScreem去。下面来对比下

    3.3.1:

    //new 一个堆栈对象
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(ResultActivity.class);
            Intent intent = new Intent(context, ResultActivity.class);
    
            //把这个intent放到栈顶,这样设置之后我们通过Notification跳转到Activity后,点击back键的时候直接回到Home Screen.
            stackBuilder.addNextIntent(intent);
    
            PendingIntent resultPending = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            notifiBuilder.setContentIntent(resultPending);

    ResultActivity在AndroidManifest.xml中

            <activity android:name="org.pqp.notification.ResultActivity" >
            </activity>

    3.3.2

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    Intent resultIntent = new Intent(context, ResultActivity1.class);
    // 添加后台堆栈 stackBuilder.addParentStack(ResultActivity1.class); // 添加Intent到栈顶 stackBuilder.addNextIntent(resultIntent); // 获得一个PendingIntent包含整个后台堆栈 containing the entire back stack PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder( context);

    前面基本上是一样的,主要是ResultActivity1在AndroidManifest.xml中声明的不一样

       <activity
                android:name="org.pqp.notification.ResultActivity1"
                android:parentActivityName=".MainActivity" >
            </activity>
    <!-- android 4.1和更高版本 -->
            <!-- android:parentActivityName=".MainActivity"> -->
            <!-- android 4.0.3和更早版本 -->
            <!--
             <meta-data 
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
            -->

    result : 3.3.1会回到HomeScreen。而3.3.2会回到MainActivity。

    还有许多用法这里就不一一介绍了,博客到这里就end了。最后附上项目app,Android_notification点击下载.

    在生活和学习中通过博客来记录自己成才是一种快乐,微笑的面对生活,未来就在前方。

  • 相关阅读:
    Codeforces Gym 101138 D. Strange Queries
    BZOJ 4236: JOIOJI
    BZOJ 2654: tree
    POJ 1390 Block
    2048
    BZOJ 2412: 电路检修
    BZOJ 2448: 挖油
    BZOJ 3907: 网格
    Codeforces 727 D T-shirts Distribution
    BZOJ 1485: [HNOI2009]有趣的数列
  • 原文地址:https://www.cnblogs.com/pengqinping/p/androidNotification.html
Copyright © 2011-2022 走看看