文章目录
- Service简介
- 启动和停止Service
- Activity和Service进行通信
Service简介
新建一个ServiceTest项目,然后右击com.example.servicetest→New→Service→Service。
每个Service中最常用到onCreate()、onStartCommand()和onDestroy()这3个方法其中onCreate()方法会在Service创建的时候调用,onStartCommand()方法会在每次Service启动的时候调用,onDestroy()方法会在Service销毁的时候调用。
另外需要注意,每一个Service都需要在AndroidManifest.xml文件中进行注册才能生效。
启动和停止Service
修改activity_main.xml中的代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/startServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Start Service" /><Buttonandroid:id="@+id/stopServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Stop Service" />
</LinearLayout>
修改MainActivity中的代码。
java">public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button startService = (Button) findViewById(R.id.startServiceBtn);Button stopService = (Button) findViewById(R.id.stopServiceBtn);startService.setOnClickListener(this);stopService.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.startServiceBtn:Intent startIntent = new Intent(this, MyService.class);startService(startIntent); // 启动服务break;case R.id.stopServiceBtn:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent); // 停止服务break;default:break;}}
}
修改MyService.java中的代码。
java">public class MyService extends Service {public MyService() {}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.throw new UnsupportedOperationException("Not yet implemented");}@Overridepublic void onCreate(){super.onCreate();Log.d("MyService", "onCreate executed");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("MyService", "onStartCommand executed");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService", "onDestroy executed");}
}
Activity和Service进行通信
修改MyService中的代码。
java">public class MyService extends Service {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;}}public MyService() {}@Overridepublic IBinder onBind(Intent intent) {return mBinder;}@Overridepublic void onCreate(){super.onCreate();Log.d("MyService", "onCreate executed");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("MyService", "onStartCommand executed");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService", "onDestroy executed");}
}
修改activity_main.xml中的代码,在布局文件里新增两个按钮用于调用Service。这两个按钮分别是用于绑定和取消绑定Service的,当一个Activity和Service绑定了之后,就可以调用该Service里的Binder提供的方法了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/startServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Start Service" /><Buttonandroid:id="@+id/stopServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Stop Service" /><Buttonandroid:id="@+id/bindServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Bind Service" /><Buttonandroid:id="@+id/unbindServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Unbind Service" />
</LinearLayout>
修改MainActivity中的代码。
java">public class MainActivity extends AppCompatActivity implements View.OnClickListener {private MyService.DownloadBinder downloadBinder;private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {downloadBinder = (MyService.DownloadBinder) service;downloadBinder.startDownload();downloadBinder.getProgress();}@Overridepublic void onServiceDisconnected(ComponentName name) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button startService = (Button) findViewById(R.id.startServiceBtn);Button stopService = (Button) findViewById(R.id.stopServiceBtn);startService.setOnClickListener(this);stopService.setOnClickListener(this);Button bindService = (Button) findViewById(R.id.bindServiceBtn);Button unbindService = (Button) findViewById(R.id.unbindServiceBtn);bindService.setOnClickListener(this);unbindService.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.startServiceBtn:Intent startIntent = new Intent(this, MyService.class);startService(startIntent); // 启动服务break;case R.id.stopServiceBtn:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent); // 停止服务break;case R.id.bindServiceBtn:Intent bindIntent = new Intent(this, MyService.class);bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务break;case R.id.unbindServiceBtn:unbindService(connection); // 解绑服务break;default:break;}}
}