Android——Notification的使用

目錄

Notification
NotificationBuilder

Notification

 private void showNotification() {
        Notification notification=new Notification();//初始化notification
        notification.icon= R.mipmap.ic_launcher;//設置通知的圖片
        notification.tickerText="我是一個消息";//設置狀態欄的文本
        notification.flags=Notification.FLAG_AUTO_CANCEL;//設置爲 可以取消
        Intent intent=new Intent(getApplicationContext(),MainActivity.class);//設置pendingIntent事件
        PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);//設置pendingInten使用方式
        notification.setLatestEventInfo(getApplicationContext(),"我是一個標題","我是內容",pend);
        notification.when=System.currentTimeMillis();//設置顯示通知的時間
        mNotificationManager.notify(1,notification); //將通知發送給id爲1的用戶
    }
    ![這裏寫圖片描述](http://img.blog.csdn.net/20150830092248962)

NotificationBuilder

    private void showNotificationBuilder() {
        Intent intent=new Intent(getApplicationContext(),MainActivity.class);//設置pendingIntent事件
        PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);//設置pendingInten使用方式
        Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("我是一條消息").setContentTitle("我是標題")
                .setContentText("我是文本吧").setContentInfo("我是消息").setShowWhen(true).setAutoCancel(true).build();
        mNotificationManager.notify(2,notification);
    }

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章