Service

Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行与后天的一种服务程序。

启动方式:

第一种:

被开启的service通过其他组件调用startService()被创建。

这种service可以无线运行下去,必须调用stopSelf方法后者其他组件调用stopService()方法停止它。

当service被停止时,系统会销毁它。

第二种 

被绑定的service是当其他组件(一个客户)调用bindservice来创建的。

客户可以通过一个IBinder接口和service进行通信。

客户可以通过unbindservice()方法来关闭这种链接。

一个service可以同时和多个绑定,当多个客户都解除绑定之后,系统会销毁service。

生命周期:

service整体的生命时间是从onCreate()被调用开始,到onDestroy()方法返回为止。和activity一样,service在onCreate()中进行它的初始化工作,在onDestroy()中释放残留资源。

比如,一个音乐播放service可以在onCreate()中创建播放音乐的线程,在onDestroy()中停止这个线程。

onCreate()和onDestroy()会被所有的service调用,不论service是通过startService()还是bindService()建立。

public void intentServicedown(View view) {
        Intent intent1 = new Intent(this,MyIntentService.class);
        Bundle bundle = new Bundle();
        bundle.putString("url","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
        bundle.putString("path","/sdcard/Movies/www.mp4");
        intent1.putExtras(bundle);
        startService(intent1);
    }
@Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        String url = extras.getString("url");
        String path = extras.getString("path");
        Log.i("---show", "onHandleIntent: "+url+path);
        try {
            URL url1 = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(2000);
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode()==200)
            {
                InputStream inputStream = httpURLConnection.getInputStream();
                int len = 0;
                byte[] buff = new byte[1024];
                FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
                int max = httpURLConnection.getContentLength();
                int progress = 0;
                while ((len = inputStream.read(buff))!= -1)
                {
                    fileOutputStream.write(buff,0,len);
                    progress+=len;
                    sendNotification(max,progress);
                }
                Intent intent1 = new Intent();
                intent1.setAction("com.bw.is");
                sendBroadcast(intent1);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendNotification(int max,int progress){
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.item);
        remoteViews.setProgressBar(R.id.pro,max,progress,false);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setCustomContentView(remoteViews);
        Notification notification = builder.build();
        startForeground(1,notification);
    }

POST

 public void postintentServicedown2(View view) {
        Intent intent1 = new Intent(this,MyIntentService3.class);
        Bundle bundle = new Bundle();
        bundle.putString("path","http://43.143.146.165:7777/foods/postFoods");
        bundle.putString("paren","pageSize=10&currentPage=1");
        intent1.putExtras(bundle);
        startService(intent1);
    }
@Override
    protected void onHandleIntent(Intent intent) {
        String path = intent.getExtras().getString("path");
        String paren = intent.getExtras().getString("paren");
        try {
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.setConnectTimeout(2000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(paren.getBytes());
            outputStream.flush();
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String str="";
                StringBuilder stringBuilder = new StringBuilder();
                while ((str=bufferedReader.readLine())!=null)
                {
                    stringBuilder.append(str);
                }
                Log.i("---bb", "onHandleIntent: "+stringBuilder.toString());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值