这一篇博客,我们开始前台服务与IntentServie的学习,关于service的生命周期及其简单使用,请参见我的博客:(android基础---->service的生命周期)
目录导航:
服务的简单说明
一、 前台服务与IntentService:
前台服务可以一直保持运行状态,而不会由于系统内存不足的原因导致被回收
service服务测试的准备代码
我们通过一个具体的案例来说明start与bind方式的service服务的生命周期的介绍。项目结果如下:
一、 在MainActivity.java中做一些初始化工作,如下代码:
private final static String TAG = "MyIntentService";privateMyIntentService.MyBinder binder;private ServiceConnection connection = newServiceConnection() {
@Overridepublic voidonServiceConnected(ComponentName name, IBinder service) {
binder=(MyIntentService.MyBinder) service;
binder.sayHello(name.getClassName());
}
@Overridepublic voidonServiceDisconnected(ComponentName name) {
Log.i(TAG,"service disconnect: " +name.getClassName());
}
};
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
二、 创建一个简单的IntentService服务类:MyIntentService
packagecom.example.linux.intentservicetest;importandroid.app.IntentService;importandroid.content.Intent;importandroid.os.Binder;importandroid.os.IBinder;importandroid.util.Log;public class MyIntentService extendsIntentService {private final static String TAG = "MyIntentService";private MyBinder myBinder = newMyBinder();class MyBinder extendsBinder {public voidsayHello(String name) {
Log.i(TAG,"say hello method: " +name);
}public voidsayWorld(String name) {
Log.i(TAG,"say world method: " +name);
}
}
@OverridepublicIBinder onBind(Intent intent) {returnmyBinder;
}publicMyIntentService() {super("MyIntentService");
Log.i(TAG,"myintent service constructor.");
}
@Overridepublic voidonCreate() {
Log.i(TAG,"on create.");super.onCreate();
}
@Overrideprotected voidonHandleIntent(Intent intent) {
Log.i(TAG,"handle intent: " + intent.getStringExtra("username") + ", thread: " +Thread.currentThread());
}
@Overridepublic voidonDestroy() {super.onDestroy();
Log.i(TAG,"on destroy.");
}
@Overridepublic int onStartCommand(Intent intent, int flags, intstartId) {
Log.i(TAG,"on start command.");return super.onStartCommand(intent, flags, startId);
}
@Overridepublic booleanonUnbind(Intent intent) {//默认返回false
String username = intent.getStringExtra("username");
Log.i(TAG,"on unbind: " + super.onUnbind(intent) + ", username: " +username);return true;
}
@Overridepublic voidonRebind(Intent intent) {
Log.i(TAG,"on rebind");super.onRebind(intent);
}
}
三、 创建一个简单的前台服务类:FrontService
packagecom.example.linux.intentservicetest;importandroid.app.Notification;importandroid.app.PendingIntent;importandroid.app.Service;importandroid.content.Intent;importandroid.os.IBinder;importandroid.util.Log;public class FrontService extendsService {private final static String TAG = "MyIntentService";publicFrontService() {
Log.i(TAG,"front service constructor");
}
@OverridepublicIBinder onBind(Intent intent) {return null;
}
@Overridepublic voidonCreate() {super.onCreate();
Notification.Builder builder= new Notification.Builder(this);
Intent intent= new Intent(this, MainActivity.class);
PendingIntent pendingIntent= PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.mipmap.ic_launcher).setTicker("ticker");
builder.setWhen(System.currentTimeMillis()).setAutoCancel(true);
builder.setContentTitle("content title").setContentText("content text");
builder.setContentIntent(pendingIntent);
Notification notify=builder.getNotification();
notify.defaults=Notification.DEFAULT_ALL;
startForeground(10, notify);
}
}
四、 在AndroidManifest.xml中注册服务与活动:
Intent服务的使用
一、 在MainActivity中创建方法,启动停止服务:
//开启服务
public voidstartService(View view) {
Intent intent= newIntent();
intent.putExtra("username", "linux");
intent.setClass(MainActivity.this, MyIntentService.class);
startService(intent);
}//停止服务
public voidstopService(View view) {
Intent intent= newIntent();
intent.setClass(MainActivity.this, MyIntentService.class);
stopService(intent);
}
二、 在MainActivity中创建方法,绑定解绑服务:
//绑定服务
public voidbindService(View view) {
Intent intent= newIntent();
intent.setClass(MainActivity.this, MyIntentService.class);
intent.putExtra("username", "linux");boolean isBind =bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.i(TAG,"bind service: " +isBind);
}//解绑服务
public voidunbindService(View view) {
Intent intent= newIntent();
intent.setClass(MainActivity.this, MyIntentService.class);
unbindService(connection);
}
三、 运行结果:
点击start:
03-25 08:01:53.460 8389-8389/? I/MyIntentService: myintent service constructor.03-25 08:01:53.460 8389-8389/? I/MyIntentService: on create.03-25 08:01:53.475 8389-8389/? I/MyIntentService: on start command.03-25 08:01:53.477 8389-8727/? I/MyIntentService: handle intent: linux, thread: Thread[IntentService[MyIntentService],5,main]03-25 08:01:53.478 8389-8389/? I/MyIntentService: on destroy.
点击stop:无输出
点击bind:
03-25 08:02:25.421 8389-8389/? I/MyIntentService: bind service: true
03-25 08:02:25.422 8389-8389/? I/MyIntentService: myintent service constructor.03-25 08:02:25.422 8389-8389/? I/MyIntentService: on create.03-25 08:02:25.432 8389-8389/? I/MyIntentService: say hello method: com.example.linux.intentservicetest.MyIntentService
点击unbind:
03-25 08:02:28.486 8389-8389/? I/MyIntentService: on unbind: false, username: linux03-25 08:02:28.490 8389-8389/? I/MyIntentService: on destroy.
前台服务的使用
一、 在MainActivity中创建方法,启动前台服务:
//前台服务的使用
public voidfrontService(View view) {
Intent intent= newIntent();
intent.setClass(MainActivity.this, FrontService.class);
startService(intent);
}
二、 运行结果: 在手机的通知栏中
友情链接
关于服务的生命周期与简单使用: (android基础---->service的生命周期)
服务的测试源代码: 访问密码 a840