Android -- 系统和自定义Notification

简介:

Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径。

作为UI部分,Notification对移动设备来说是最适合不过的了。用户可能随时都带着手机在身边。一般来说,用户会在后台打开几个程序,但不会注意它们。在这样的情形下,当发生需要注意的事件时,能够通知用户是很重要的。

Notification由NotificationManger统一管理,目前包含的能力有:

  • 创建一个状态条图标。
  • 在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。
  • 闪灯或LED。
  • 电话震动。
  • 发出听得见的警告声(铃声,保存的声音文件)。

调用系统API的Notification                                                       

复制代码
public void click1(View view) {
        //1.获取系统通知的管理者
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //2.初始化一个notification的对象
         Notification notification = new Notification(R.drawable.notification, "我是滚动的文本",  System.currentTimeMillis() );
        //3.设置notification的具体参数
         notification.flags = Notification.FLAG_AUTO_CANCEL;
        // notification.sound = Uri.parse(uriString);
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_VIEW);
         intent.setData(Uri.parse("http://www.baidu.com"));
         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
         notification.setLatestEventInfo(this, "我是大的标题", "我是标题下面的内容", contentIntent);
         //4.直接把消息给 notification的管理者
         nm.notify(0, notification);
    }
复制代码

自定义Notification                                                                     

复制代码
public class MainActivity extends Activity {

    protected int mProgress;
    private Notification notification;
    private RemoteViews rv;
    private NotificationManager nm;
    private PendingIntent contentIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void clicklala(View view)
    {
        //1.获取系统通知的管理者
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //2.初始化一个notification的对象
        long when = System.currentTimeMillis();
        String tickerText = "iiiipppp";
        notification = new Notification(R.drawable.ic_launcher,tickerText,when);
        //3.设置notification的具体参数
        //不能手动清理掉
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        // notification.contentView = 自定义的notification view
        rv = new RemoteViews(getPackageName(), R.layout.notify);
        rv.setTextViewText(R.id.tv_rv, "我是自定义的notification");
        rv.setProgressBar(R.id.pb_rv, 100, 0, false);
        notification.contentView = rv;

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification.contentIntent = contentIntent;
        
        //4.直接把消息给 notification的管理者
        nm.notify(0, notification);

        // 启动多线程更新Notification提醒。 
        new Thread(new Runnable() {  
            public void run() {  
                for (int i = 0; i < 20; i++) {  
                    mProgress = (i + 1) * 5;  
                    try {  
                        if (i < 19) {  
                            Thread.sleep(1000);  
                        } else {  
                            Thread.currentThread().interrupt();  
                        }  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    Message message = new Message();  
                    mHandler.sendMessage(message);  
                }  
            }  
        }).start();
    }
    private Handler mHandler = new Handler() {  
        public void handleMessage(Message message) {  

            //设置RemoteView组件中进度条的进度值。 
            rv.setProgressBar(R.id.pb_rv, 100, mProgress, false);  
            rv.setTextViewText(R.id.tv_rv, "Download:" + mProgress + "%");  
            //给Notification设置布局。  
            notification.contentView = rv;  
            /给Notification设置Intent,单击Notification会发出这个Intent。 
            notification.contentIntent = contentIntent;  
            //发送Notification提醒。   
            nm.notify(0, notification);  
            super.handleMessage(message);  
        }  
    };  
}
复制代码

布局:

复制代码
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent">  
 <TextView   
  android:id="@+id/tv_rv"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:text="haha"  
 />   
<ProgressBar   
  style="@android:style/Widget.ProgressBar.Horizontal"  
  android:id="@+id/pb_rv"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
 />   
</LinearLayout>
复制代码

我是天王盖地虎的分割线                                                              

3

 

 

 

源代码:http://pan.baidu.com/s/1dD1Qx01

notification入门.zip

 

 




本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/3828941.html,如需转载请自行联系原作者

相关文章
|
3天前
|
机器学习/深度学习 Java Shell
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
19 0
|
1天前
|
XML Android开发 数据格式
Android下自定义Button样式
Android下自定义Button样式
|
1天前
|
Android开发
Android 如何将定制的Launcher成为系统中唯一的Launcher
Android 如何将定制的Launcher成为系统中唯一的Launcher
11 2
|
1天前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
|
1天前
|
搜索推荐 Android开发
自定义Android标题栏TitleBar布局
自定义Android标题栏TitleBar布局
|
3天前
|
Java Android开发
Android OTG U盘无法显示在系统文件管理的修改
Android OTG U盘无法显示在系统文件管理的修改
5 0
|
3天前
|
安全 Android开发
修改Android系统的签名
修改Android系统的签名
14 0
|
3天前
|
安全 Android开发
Android 系统签名
Android 系统签名
13 0
|
3天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
10 0
|
3天前
|
Android开发
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
11 0