这两天上网学习了一下服务、广播接收器和通知,在这里记录一下所得

来源:互联网 发布:淘宝店代管理 编辑:程序博客网 时间:2024/05/17 08:40

第一次记录自己的所学,也希望能帮到其他像我这样的新手。。

好了,回归正题,我在网上学习也自己写了一个小玩意,是根据服务、广播、通知3者结合起来的,直接上代码吧:

首选我定义了一个Button

       button.setOnClickListener(new OnClickListener() {  
            
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
           
            if(isClick == false){
            num = num + 1;
            isClick = true;
            logger.i("isClick222222 = " + isClick);
            progress = 0;
               //在这里启动服务  
               Intent intent  = new Intent();  
               intent.putExtra("progress", progress);  
               intent.setClass(BroadcastReceiverTestActivity.this, LocalService.class);  
               startService(intent); 
            }
            }  
        }); 

点击这个Button会开启一个后台服务

       public void onStart(Intent intent, int startId) {  
        // TODO Auto-generated method stub  
        super.onStart(intent, startId);  
        //得到Activity中传递过来的数据  
        progress = intent.getIntExtra("progress", 0);  
        //创建一个线程,每隔一秒向Activity发送一个广播,并将progress的新值发送出去  
        new Thread(){  
            public void run() {  
                while(progress<100){  
                    progress = progress+1;  
                    Intent intent = new Intent();  
                    intent.putExtra("serviceProgress", progress);  
                    //设置发送广播的类型,可以随便写一个  
                    intent.setAction("com.example.service");  
                    sendBroadcast(intent);  
                    try {  
                        sleep(1000);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    } 
                }  
            }  
        }.start();  
    }  

服务每隔一秒progress就会加1,然后发送广播,再在另外一个Activity里面接收这个广播

    //定义一个广播接收器  
    class MyBroadcastReceiver extends BroadcastReceiver{  


@Override  
        public void onReceive(Context context, Intent intent) {  
// logger.d();
            // TODO Auto-generated method stub  
            //接收到Service发送的广播信息,得到数据,更新UI  
            progress = intent.getIntExtra("serviceProgress", 0);  
            showProgressBar();
            progressBar1.setProgress(progress);
            button.setText(progress + "%");
            if(progress == 100){
            isClick = false;
            }
            
        }  
    }  

通过这个广播接收器接收到的progress 值,进行更新UI,showProgressBar()里面是通知的内容

    private void showProgressBar() {
   
//     logger.d();
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //将setProgress的第三个参数设为true即可显示为无明确进度的进度条样式
builder.setSmallIcon(R.drawable.ic_launcher)
                .setTicker("showProgressBar").setContentInfo((progress) + "%")
                .setOngoing(true).setContentTitle("Notification"+num)
                .setContentText("正在加载中...").setProgress(100, progress, false);
if(progress == 100){
        builder.setContentText("加载完成").setProgress(0, 0, false).setOngoing(false);
        }

//点击消失
builder.setAutoCancel(true);

Intent intent = new Intent(this,TwoActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("num", num);
builder.setContentIntent(PendingIntent.getActivity(this, num, intent, 0));

// //这句和"点击消失"那句是“Notification点击消失但不会跳转”的必须条件,如果只有"点击消失"那句,这个功能是不能实现的
// builder.setContentIntent(PendingIntent.getActivity(this, num, new Intent(), 0));
manager.notify(num, builder.build());


    }

这里写了一个带进度条的通知样式,根据广播接收器接收到的progress值更新进度条

                Intent intent = new Intent(this,TwoActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("num", num);
builder.setContentIntent(PendingIntent.getActivity(this, num, intent, 0));

在这里我们进行点击通知进行跳转的动作

恩。。语言组织能力有限,就这样吧,我研究研究怎么上传代码

再加上资源下载链接

http://download.csdn.net/detail/zhumj_zhumj/8573909






   





    

0 0