zoukankan      html  css  js  c++  java
  • Notification的功能与用法

          Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态、时间等。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

         提示:NotificationManager是一个重要的系统服务,该API位于android的应用程序框架层,应用程序可通过NotificationManager向系统发送全局通知。

         Android 3.0增加Notification.Builder类,通过该类允许开发者更轻松的创建Notification对象。Notification.Builder提供了如下常用方法。

    • setDefaults():设置通知LED灯、音乐、振动等。      
    • setAutoCancel():设置点击通知后,状态栏自动删除通知。
    • setContentTitle():方法设置通知标题。
    • setContentText():设置通知内容。
    • setSmallIcon():未通知设置图标。
    • setLargeIcon():未通知设置大图标。
    • setTick():设置通知在状态栏的提示文本。
    • setContentIntent():设置点击通知后将要启动的程序组件对应的PendingIntent。

         发送Notification很简单,按如下步骤进行即可。

    1. 调用getSystemService(NOTFICATION_SERVICE)方法获取系统的NotificationManager服务。
    2. 通过构造器创建一个Notification对象。
    3. 为Notification设置各种属性。
    4. 通过NotificationManager发送Notification。

          实例:加薪通知    

          本实例示范了如何通过NotificationManager来发送、取消Notification,本实例的界面很简单,只是包含两个普通按钮,分别用于发送Notification和取消Notification。

          本示例的界面布局文件如下:

         

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        >
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送Notification"
        android:onClick="send"/>
     <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除Notification"
        android:onClick="del"/>
    
    </LinearLayout>

    该Activity程序的后台代码文件如下:
     

    package org.crazyit.helloworld;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
    import android.app.Notification.Builder;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    
    public class NotificationTest extends Activity {
    
        static final int NOTIFICATION_ID =0x123;
        NotificationManager nm;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notification_test);
            //获取系统的NotificationManager服务
            nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        }
      //为发送通知的按钮的点击事件定义事件处理方法
        public void send(View source)
        {
            //创建一个启动其他Activity的Intent
            Intent intent=new Intent(NotificationTest.this,OtherActivity.class);
            PendingIntent pi=PendingIntent.getActivity(NotificationTest.this,
                    0, intent, 0);
            Notification notify=new Notification.Builder(this)
            //设置打开该通知,该通知自动消失
            .setAutoCancel(true)
            //设置显示在状态栏的通知提示信息
            .setTicker("有新消息")
            //设置通知的图标
            .setSmallIcon(R.drawable.notify)
            //设置通知内同的标题
            .setContentTitle("一条新通知")
            .setContentText("恭喜您,您加薪了,工资增加20%!")
            //设置系统默认的声音、震动
            //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
            //设置通知的自定义声音
            .setSound(Uri.parse("android.resource://org.craztit.ui"+R.raw.msg))
            .setWhen(System.currentTimeMillis())
            //设置盖通知将要启动程序的Intent
            .setContentIntent(pi)
            .build();
            //发送通知
            nm.notify(NOTIFICATION_ID,notify);
        }
        //为删除通知的按钮的点击事件定义事件处理方法
        public void del(View v)
        {
            //取消通知
            nm.cancel(NOTIFICATION_ID);
        }
        
        
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.notification_test, menu);
            return true;
        }
    
    }

       上面的程序中粗体字代码用于为Notification设置各种属性,包括Notification的图标、标题、发送时间等。除此之外,上面的程序还通过setDefaults()方法为Notification设置了声音提示、振动提示、闪光灯等。该属性支持如下属性值。

    • DEFAULT_SOUND:设置使用默认声音。
    • DEFAULT_VIBRATE:设置默认振动。
    • DEFAULT_LIGHTS:设置使用默认闪光灯。
    • ALL:设置使用默认声音、振动、闪光灯。

         如果不想使用默认设置,也可以使用如下代码:

       

    //设置自定义声音
    setSound("file///sdcard/click.mp3");
    //设置自定义振动
    setVibrate(new long[]{0,50,100,150});

       运行上面的程序,单击程序中“发送Notification”按钮,将可以看到手机屏幕上方出现了一个Notification。将状态栏向下拖动,将可以看到Notification的详情,

    上图中Notification还关联了一个Activity:OtherActivity,因此当用户单击“通知”时即可启动OtherActivity——OtherActivity是一个十分简单的程序,故此处不再介绍。

    由于上面的程序指定了该Notification要启动OtherActivity,因此一定不要忘记在AndroidManifest.xml文件中声明该Activity。而且上面的程序中还需要访问系统闪光灯、振动器,这也需要在AndroidManifest.xml文件中声明权限。也就是增加如下代码片段即可。

     <!-- 添加操作闪光灯的权限 -->
        <uses-permission android:name="android.permission.FLASHLIGHT" />
        <!-- 添加操作振动器的权限 -->
        <uses-permission android:name="android.permission.VIBRATE" />
    
      <activity
                android:name="org.crazyit.helloworld.OtherActivity"
                android:label="@string/title_activity_other" >
            </activity>

     

         

  • 相关阅读:
    阿里宣布成立云原生技术委员会,释放哪些趋势信息?
    自动生成低精度深度学习算子
    将TVM集成到PyTorch上
    自定义Kubernetes调度程序来编排高可用性应用程序
    Linux实现ffmpeg H.265视频编码
    iOS视频硬编码技术
    TVM虚拟机配置
    基于Jittor框架实现LSGAN图像生成对抗网络
    Angel图算法
    AIFramework框架Jittor特性(下)
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3392764.html
Copyright © 2011-2022 走看看