IntentService是继承自Service实现了异步功能的类。
创建服务,并重写方法
启动服务,下载数据
下载成功后使用广播接收者收内容并显示
public class ServicesActivity extends AppCompatActivity {
Intent intent;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
//参数一:上下文 参数二:自定义服务类.class
intent = new Intent(this,MyServices.class); }
public void startFroatServer(View view) {
startService(intent);
}
public void stopFroatServer(View view) {
stopService(intent); }