android 四大组件之Service 结合通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 四大组件之Service 结合通知相关的知识,希望对你有一定的参考价值。

   由于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 结合通知的主要内容,如果未能解决你的问题,请参考以下文章

Android四大组件之service

Android四大组件service之Bound Service

android 四大组件之---Service

Android四大组件之Service

android 四大组件之Service 结合通知

Android 四大组件之Service学习