下载音乐

package com.bw.day15;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //动态权限
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },101);
        }
        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bw.day15");
        registerReceiver(myReceiver,intentFilter);

    }

    public void forqian(View view){
        Intent intent = new Intent(this, MyService.class);
        startService(intent);

    }

    public void intentservice(View view){
        Intent intent = 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/cai.mp4");
        intent.putExtras(bundle);
        startService(intent);
    }

    public void getqing(View view){
        Intent intent = new Intent(this, MyIntentService2.class);
        Bundle bundle = new Bundle();
        bundle.putString("url","http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=10");
        bundle.putString("path","/sdcard/Movies/json.txt");
        intent.putExtras(bundle);
        startService(intent);

    }

    public void post(View view){
        Intent intent = new Intent(this, MyIntentService3post.class);
        Bundle bundle = new Bundle();
        bundle.putString("urls","http://43.143.146.165:7777/foods/postFoods");
        bundle.putString("params","pageSize=10&currentPage=1");
        intent.putExtras(bundle);
        startService(intent);
    }



}

服务

package com.bw.day15;

import android.app.IntentService;
import android.app.Notification;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.RemoteViews;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("---service","handleInt");

        Bundle extras = intent.getExtras();
        String url = extras.getString("url");
        String path = extras.getString("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();
                byte[] bytes = new byte[1024];
                int len=0;
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    File file = new File(path);
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    int max=httpURLConnection.getContentLength();
                    int progress=0;
                    while ((len=inputStream.read(bytes))!=-1){
                        fileOutputStream.write(bytes,0,len);
                        progress+=len;
                        dasas(max,progress);
                    }
                    Intent intent1 = new Intent();
                    intent1.setAction("com.bw.day15");
                    sendBroadcast(intent1);
                }

            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

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


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值