定义: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(); } } }