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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM