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

原文地址:https://www.cnblogs.com/yydcdut/p/3828941.html