IntentService
IntentService是继承自Service实现了异步功能的类。
Service是Android系统中的四大组件之一。
后台运行和跨进程访问。
Service可以在后台执行长时间运行操作
服务由其他应用组件启动,用户切换到其他应用,服务仍将在后台继续运行
IntentService-应用
第一步:新建一个MyService继承自IntentService
public class MyIntentService2 extends IntentService {
public MyIntentService2() {
super("MyIntentService2");
}
@Override
protected void onHandleIntent(Intent intent) {
}
}
第二步:开启服务
public void start(View view) {
Intent intent=new Intent(this,MyforeproService.class);
startService(intent);
}
//在activity中开启服务
Service-前台服务
创建Service重写方法
publicclass MyServices extends Service {
private static final String TAG = "amy";
@Override
public void onCreate(){
super.onCreate();
Log.d(TAG,"onCreate");
}
启动服务
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.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
},101);
}
}
public void start(View view) {
Intent intent=new Intent(this,MyforeproService.class);
startService(intent);
}
public void stop(View view) {
Intent intent=new Intent(this,MyforeproService.class);
stopService(intent);
}
public void staris(View view) {
Intent intent=new Intent(this,MyIntentService.class);
Bundle bundle=new Bundle();
bundle.putString("u","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
bundle.putString("s","/sdcard/Movies/dog.mp4");
intent.putExtras(bundle);
startService(intent);
}
}
Notification+IntentService
发送通知
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
public void onCreate() {
super.onCreate();
Log.i("---is","onCreate");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle bundle=intent.getExtras();
String u=bundle.getString("u");
String s=bundle.getString("s");
try {
URL url=new URL(u);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(2000);
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.connect();
if(httpURLConnection.getResponseCode()==200){
InputStream inputStream=httpURLConnection.getInputStream();
byte[] buffer=new byte[1024];
File file=new File(s);
int len=0;
FileOutputStream fileOutputStream=new FileOutputStream(file);
int max=httpURLConnection.getContentLength();
int progress=0;
while ((len=inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer,0,len);
progress+=len;
sendno(max,progress);
}
Intent intent1=new Intent();
intent1.setAction("com.bw.ok");
sendBroadcast(intent1);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("---is","onHand");
}
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.pd,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 void staris(View view) {
Intent intent=new Intent(this,MyIntentService.class);
Bundle bundle=new Bundle();
bundle.putString("u","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
bundle.putString("s","/sdcard/Movies/dog.mp4");
intent.putExtras(bundle);
startService(intent);
}