码迷,mamicode.com
首页 > 移动开发 > 详细

android 四大组件之Service(7) 结合通知

时间:2016-06-25 16:32:58      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

   由于Service运行在后台, 一旦运行,使用Toast Notifications 和 Status Bar Notification

来通知客户。

Service结合通知和用户交互:

public class DownloadService extends Service {
    private String uri;
    //通知管理器
    private NotificationManager nmanager;
    private Notification.Builder builder ;
    @Override
    public void onCreate() {
        super.onCreate();
        getNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        uri = intent.getStringExtra("uri");
        //启动下载进程
        new Thread(new MyRunnable()).start();
        return START_STICKY ;
    }
    /**
     *  下载放在线程中执行,
     *  
     */
    public class MyRunnable implements Runnable {
        @Override
        public void run() {
            //读取的数据存储
            byte[]  result = null;
            HttpClient client = new DefaultHttpClient();
            try {
                Log.i("tag", "uri>>"+uri);
                HttpGet get = new HttpGet(uri);
                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                int stateCode = response.getStatusLine().getStatusCode();
                if(stateCode != 200){
                    Log.i("tag", stateCode+"-->下载失败-->");
                }
                //获取网络输入流
                InputStream in = entity.getContent();
                //获取网路数据总长度
                long total  = entity.getContentLength();
                byte[] bs = new byte[10];
                int len_per = 0;//每次读取的长度,
                ByteArrayOutputStream baos = new  ByteArrayOutputStream();
                int readedLen = 0;//已经读取的数据量
                while((len_per = in.read(bs)) != -1){
                    baos.write(bs, 0, len_per);
                    readedLen += len_per;
            //将进度消息发送给handler, 在handler中更新进度
int progress = (int) ((readedLen*100)/(float)total); Message msg = Message.obtain(); msg.arg1 = progress; handler.sendMessage(msg); } result = baos.toByteArray();
          //通知完成 handler.sendEmptyMessage(
1); }catch(Exception e){ e.printStackTrace(); }finally { if(client != null){ client.getConnectionManager().shutdown(); } } } } @Override public IBinder onBind(Intent intent) { return null; } /** * 更新UI */ private Handler handler = new Handler() { public void handleMessage(Message msg) { //更新UI builder.setProgress(100, msg.arg1, false); nmanager.notify(1001, builder.build()); if(msg.what == 1){ //下载完成 Toast.make(getapplicationContext(), "下载完成", 1).show();s } } }; /** *通知初始化 */ private void getNotification() { nmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); builder = new Notification.Builder(getApplicationContext()); builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker("通知来了"); builder.setContentTitle("下载"); builder.setContentText("正在下载。。。。。"); } }

 

       

android 四大组件之Service(7) 结合通知

标签:

原文地址:http://www.cnblogs.com/chengbao/p/5616392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!