服务:是Android中实现程序后台运行的解决方案,适合去执行那些不需要和用户而且还要求长期运行的任务。
定义一个服务
onCreate()方法会在服务创建的时候调用
onStartCommand()方法会在每次服务启动的时候调用
onDestroy()方法会在服务销毁的时候调用
MyService.java
public class MyService extends Service { public MyService() { } private DownloadBinder mBinder = new DownloadBinder(); class DownloadBinder extends Binder { public void startDownload() { Log.d("MyService","startDownload executed"); } public int getProgress() { Log.d("MyService","getProgress executed"); return 0; } } @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { super.onCreate(); Log.d("MyService","onCreate executed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService","onStartCommand executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("MyService","onDestroy executed"); } }
启动服务
Intent startIntent = new Intent(this, MyService.class); startService(startIntent);
停止服务
Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent);
活动和服务进行通信
创建一个专门的Binder对象来对下载功能进行管理
修改MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private MyService.DownloadBinder downloadBinder; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { downloadBinder = (MyService.DownloadBinder)service; downloadBinder.startDownload(); downloadBinder.getProgress(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startService = (Button)findViewById(R.id.start_service); Button stopService = (Button)findViewById(R.id.stop_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); Button bindService = (Button)findViewById(R.id.bind_service); Button unbindService = (Button)findViewById(R.id.unbind_service); bindService.setOnClickListener(this); unbindService.setOnClickListener(this); Button startIntentService = (Button)findViewById(R.id.start_intent_service); startIntentService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_service: Intent startIntent = new Intent(this, MyService.class); startService(startIntent); break; case R.id.stop_service: Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent); break; case R.id.bind_service: Intent bindIntent = new Intent(this,MyService.class); bindService(bindIntent,connection,BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; case R.id.start_intent_service: Log.d("MainActivity","Thread id is " + Thread.currentThread().getId()); Intent intentService = new Intent(this,MyIntentService.class); startService(intentService); break; default: break; } } }
创建一个ServiceConnection匿名类,重写onServiceConnected和onServiceDisconnected方法,会在活动与服务成功绑定与断开时调用。
IntentService
服务中的代码都是默认运行在主线程中。Android专门提供了一个IntentService类,创建一个异步的,会自动停止的服务。
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(@Nullable Intent intent) { Log.d("MyIntentService","Thread id is" + Thread.currentThread().getId()); } @Override public void onDestroy() { super.onDestroy(); Log.d("MyIntentService","onDestroy executed"); } }
启动方式
Intent intentService = new Intent(this, MyIntentService.class); startService(intentService);