day15后台操作及IntentService

定义:IntentService是继承自Service实现了异步功能的类。

第一步:新建一个MyService继承自Service

 

第二步:开启服务

 

 

public void startfor(View view) {
    Intent intent = new Intent(this,MyforgroundService.class);
    startService(intent);
}

public void stopfor(View view) {
    Intent intent = new Intent(this,MyforgroundService.class);
    stopService(intent);
}

public void startis(View view) {
    Intent intent = new Intent(this,MyIntentService.class);
    Bundle bundle = new Bundle();
    bundle.putString("urlpath","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
    bundle.putString("sdpath","sdcard/Movies/dog.mp4");
    intent.putExtras(bundle);
    startService(intent);
}
public class MyforgroundService extends Service {
    public MyforgroundService() {
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.item);
        remoteViews.setTextViewText(R.id.title_tv,"歌名");
        remoteViews.setTextViewText(R.id.artist_tv,"歌手");
        remoteViews.setImageViewResource(R.id.ivvv,R.drawable.ic_launcher_background);
        Intent intent1 = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,101,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setCustomContentView(remoteViews)
                .setContentIntent(pendingIntent);
        Notification notification=builder.build();
        notificationManager.notify(1,notification);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }



    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("---is", "onCreate: ");
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle bundle=intent.getExtras();
        String urlpath=bundle.getString("urlpath");
        String sdpath=bundle.getString("sdpath");
        try {
            URL url=new URL(urlpath);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(2000);
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode()==200)
            {
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] buffer = new byte[1024];
                int len=0;
                int max=httpURLConnection.getContentLength();
                int progress = 0;
                File file=new File(sdpath);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                while((len=inputStream.read(buffer))!=-1)
                {
                    fileOutputStream.write(buffer,0,len);
                    progress+=len;
                    sendno(max,progress);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("---is", "onHandleIntent: ");
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void sendno(int max, int progress)
    {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        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)
                .setCustomContentView(remoteViews);
        Notification notification = builder.build();
        notificationManager.notify(1,notification);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("---is", "onDestroy: ");
    }

}

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.bw.ok"))
        {
            Toast.makeText(context, "下载完毕", Toast.LENGTH_SHORT).show();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值