在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → Notifications(通知栏学习)

Notifications(通知栏学习)

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:3.02M
  • 下载次数:7
  • 浏览次数:232
  • 发布时间:2015-09-25
  • 实例类别:Android平台开发
  • 发 布 人:黎佳
  • 文件格式:.rar
  • 所需积分:5
 相关标签: Notifications(通知栏学习)

实例介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.example.notifications;
 
import android.app.Notification;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
 
import com.example.notifications.base.BaseActivity;
 
public class ProgressAcitivty extends BaseActivity implements OnClickListener {
    private Button btn_show_progress;
    private Button btn_show_un_progress;
    private Button btn_show_custom_progress;
    /** Notification的ID */
    int notifyId = 102;
    /** Notification的进度条数值 */
    int progress = 0;
    NotificationCompat.Builder mBuilder;
    Button btn_download_start;
    Button btn_download_pause;
    Button btn_download_cancel;
    /** 下载线程是否暂停 */
    public boolean isPause = false;
    /** 通知栏内是否是自定义的 */
    public boolean isCustom = false;
    DownloadThread downloadThread;
    /** true:为不确定样式的   false:确定样式*/
    public Boolean indeterminate = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress);
        initView();
        initNotify();
    }
 
    private void initView() {
        btn_show_progress = (Button) findViewById(R.id.btn_show_progress);
        btn_show_un_progress = (Button) findViewById(R.id.btn_show_un_progress);
        btn_show_custom_progress = (Button) findViewById(R.id.btn_show_custom_progress);
        btn_download_start = (Button) findViewById(R.id.btn_download_start);
        btn_download_pause = (Button) findViewById(R.id.btn_download_pause);
        btn_download_cancel = (Button) findViewById(R.id.btn_download_cancel);
        btn_show_progress.setOnClickListener(this);
        btn_show_un_progress.setOnClickListener(this);
        btn_show_custom_progress.setOnClickListener(this);
        btn_download_start.setOnClickListener(this);
        btn_download_pause.setOnClickListener(this);
        btn_download_cancel.setOnClickListener(this);
    }
 
    /** 初始化通知栏 */
    private void initNotify() {
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
                .setContentIntent(getDefalutIntent(0))
                // .setNumber(number)//显示数量
                .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
                // .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)// ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE)// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
                // Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 //
                // requires VIBRATE permission
                .setSmallIcon(R.drawable.icon);
    }
 
    /** 显示带进度条通知栏 */
    public void showProgressNotify() {
        mBuilder.setContentTitle("等待下载")
                .setContentText("进度:")
                .setTicker("开始下载");// 通知首次出现在通知栏,带上升动画效果的
        if(indeterminate){
            //不确定进度的
            mBuilder.setProgress(0, 0, true);
        }else{
            //确定进度的
            mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条  设置为true就是不确定的那种进度条效果
        }
        mNotificationManager.notify(notifyId, mBuilder.build());
    }
     
    /** 显示自定义的带进度条通知栏 */
    private void showCustomProgressNotify(String status) {
        RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_progress);
        mRemoteViews.setImageViewResource(R.id.custom_progress_icon, R.drawable.icon);
        mRemoteViews.setTextViewText(R.id.tv_custom_progress_title, "今日头条");
        mRemoteViews.setTextViewText(R.id.tv_custom_progress_status, status);
        if(progress >= 100 || downloadThread == null){
            mRemoteViews.setProgressBar(R.id.custom_progressbar, 0, 0, false);
            mRemoteViews.setViewVisibility(R.id.custom_progressbar, View.GONE);
        }else{
            mRemoteViews.setProgressBar(R.id.custom_progressbar, 100, progress, false);
            mRemoteViews.setViewVisibility(R.id.custom_progressbar, View.VISIBLE);
        }
        mBuilder.setContent(mRemoteViews)
                .setContentIntent(getDefalutIntent(0))
                .setTicker("头条更新");
        Notification nitify = mBuilder.build();
        nitify.contentView = mRemoteViews;
        mNotificationManager.notify(notifyId, nitify);
    }
     
    /** 开始下载 */
    public void startDownloadNotify() {
        isPause = false;
        if (downloadThread != null && downloadThread.isAlive()) {
//          downloadThread.start();
        }else{
            downloadThread = new DownloadThread();
            downloadThread.start();
        }
    }
 
    /** 暂停下载 */
    public void pauseDownloadNotify() {
        isPause = true;
        if(!isCustom){
            mBuilder.setContentTitle("已暂停");
            setNotify(progress);
        }else{
            showCustomProgressNotify("已暂停");
        }
    }
 
    /** 取消下载 */
    public void stopDownloadNotify() {
        if (downloadThread != null) {
            downloadThread.interrupt();
        }
        downloadThread = null;
        if(!isCustom){
            mBuilder.setContentTitle("下载已取消").setProgress(0, 0, false);
            mNotificationManager.notify(notifyId, mBuilder.build());
        }else{
            showCustomProgressNotify("下载已取消");
        }
    }
 
    /** 设置下载进度 */
    public void setNotify(int progress) {
        mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条
        mNotificationManager.notify(notifyId, mBuilder.build());
    }
 
    /**
     * 下载线程
     */
    class DownloadThread extends Thread {
 
        @Override
        public void run() {
            int now_progress = 0;
            // Do the "lengthy" operation 20 times
            while (now_progress <= 100) {
                // Sets the progress indicator to a max value, the
                // current completion percentage, and "determinate"
                if(downloadThread == null){
                    break;
                }
                if (!isPause) {
                    progress = now_progress;
                    if(!isCustom){
                        mBuilder.setContentTitle("下载中");
                        if(!indeterminate){
                            setNotify(progress);
                        }
                    }else{
                        showCustomProgressNotify("下载中");
                    }
                    now_progress  = 10;
                }
                try {
                    // Sleep for 1 seconds
                    Thread.sleep(1 * 1000);
                } catch (InterruptedException e) {
                }
            }
            // When the loop is finished, updates the notification
            if(downloadThread != null){
                if(!isCustom){
                    mBuilder.setContentText("下载完成")
                    // Removes the progress bar
                    .setProgress(0, 0, false);
                    mNotificationManager.notify(notifyId, mBuilder.build());
                }else{
                    showCustomProgressNotify("下载完成");
                }
            }
        }
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_show_progress:
            downloadThread = null;
            isCustom = false;
            indeterminate = false;
            showProgressNotify();
            break;
        case R.id.btn_show_un_progress:
            downloadThread = null;
            isCustom = false;
            indeterminate = true;
            showProgressNotify();
            break;
        case R.id.btn_show_custom_progress:
            downloadThread = null;
            isCustom = true;
            indeterminate = false;
            showCustomProgressNotify("等待下载..");
            break;
        case R.id.btn_download_start:
            startDownloadNotify();
            break;
        case R.id.btn_download_pause:
            pauseDownloadNotify();
            break;
        case R.id.btn_download_cancel:
            stopDownloadNotify();
            break;
        default:
            break;
        }
    }
}

实例下载地址

Notifications(通知栏学习)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警