android四大组件 显示界面,android四大组件之Service

最近因为还没找到工作所以也趁着现在有时间,将以前的只是整理下,要不然总容易遗忘,今天就来讲解下Service的用法。作为Android的四大组件之一,其重要性可想而知。在应用中我们主要是用来进行一些后台操作,不需与应用UI进行交互,执行耗时任务等。

官方文档中这样说:

Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。

此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O

或与内容提供程序交互,而所有这一切均可在后台进行。

Service的用途:

1.在后台执行耗时操作,但不需要与用户进行交互。2.一个应用暴露出来的一些供其他应用使用的功能。

这里需要声明一点,Service是运行在主线程中,因而如果需要进行耗时操作或者访问网络等操作,需要在Service中再开启一个线程来执行(使用IntentService的话则不需要在自己手动开启线程)。

启动Service

启动一个Service有两种方式:

Context.startService()

Context.bindService()

36315ecaa045e37f068ad9ddda9dc00e.png

startService()方式启动Service,我们启动之后是没有办法再对Service进行控制的,而且启动之后该Service是一直在后台运行的,即使它里面的一些代码执行完毕,我们要想终止该Service,就需要在他的代码里面调用stopSelf()方法或者直接调用stopService() 方法。而通过bindService()方法启动的Service,客户端将获得一个到Service的持久连接,客户端会获取到一个由Service的onBind(Intent)方法返回来的IBinder对象,用来供客户端回调Service中的回调方法。

我们无论使用那种方法,都需要定义一个类,让它继承Service类,并重写其中的几个方法,如果我们是采用startService()方式启动的话,只需要重写onCreate() 、onStartCommand(Intent intent, int flags, int startId)、onDestroy()方法即可(其实我们也可以重写),而如果采用的是bindService()方法启动的话,我们就需要重写onCreate() 、onBind(Intent intent)、 onUnbind(Intent intent)方法.注意,作为四大组件之一,Service使用之前要在清单文件中进行配置。

......

android:name=".MyService">

Context.startService()

MyService.java的代码:

publicclass MyService extends Service {

publicMyService() {

}

@Override

publicvoid onCreate() {

super.onCreate();

Log.i("test","onCrete executed !");

}

@Override

publicintonStartCommand(Intent intent,intflags,intstartId) {

Log.i("test","onStartComand executed !");

returnsuper.onStartCommand(intent, flags, startId);

}

@Override

publicvoid onDestroy() {

super.onDestroy();

Log.i("test","onDestroy executed !");

}

}

MainActivity.java的代码如下:

publicclass MainActivity extends AppCompatActivity implementsView.OnClickListener{

Button btnStart,btnStop;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnStart = (Button) findViewById(R.id.btn_start);

btnStop = (Button) findViewById(R.id.btn_stop);

btnStart.setOnClickListener(this);

btnStop.setOnClickListener(this);

}

@Override

publicvoid onClick(Viewview) {

Intent mIntent = new Intent(MainActivity.this,MyService.class);

switch (view.getId()){

caseR.id.btn_start:

startService(mIntent);

break;

caseR.id.btn_stop:

stopService(mIntent);

break;

}

}

}

主界面就两个按钮,一个用来启动Service,一个用来停止Service:

9db7d51e70567681b9f3b7497dc0826c.png

下面我们先点击START按钮,Log信息如下:

cbf8e8a5d9f1246b27409ff99c24ee73.png

可以看出,onCreate()方法先执行,然后onStartCommand()方法紧接着执行,那么如果我们再次点击启动按钮呢?结果如下图:

6402ddc2f92f1503ef324fc412eb050e.png

我们可以看到,这次onCreate()方法没有再执行,而是直接执行了onStartCommand()方法,这是因为Service只在***次创建的时候才执行onCreate()方法,如果已经创建了,那之后再次调用startService()启动该Service的时候,只会去执行onStartCommand()方法方法,而不会再执行onCreate()方法。

接下来我们点击停止按钮,可以看到,onDestroy()方法被执行了:

7d2f558be8160f2f8f0368622c245363.png

注意,如果我们不点击停止按钮手动停止该Service的话,该Service会一直在后台运行,即使它的onStartCommand()方法中的代码已经执行完毕,在下图中我们可以看到:

这时候我们的这个Service是一直在后台执行的,即使它的onStartCommand()方法中的代码已经执行完了。如果我们想要它自动停止的话,可以将onStartCommand()方法中的代码修改如下:

@Override

publicintonStartCommand(Intent intent,intflags,intstartId) {

Log.i("test","onStartComand() executed !");

stopSelf();

returnsuper.onStartCommand(intent, flags, startId);

}

Context.bindService()

采用该方法的代码就稍微比以前的多了,因为我们需要在客户端对Service进行控制,因而会在MainActivity中创建一个匿名内部类ServiceConnection,然后会在bindService()方法和unbindService()方法中将其传入。MyService.java 中的代码如下:

publicclass MyService extends Service {

private MyBinder myBinder = new MyBinder();

publicMyService() {

}

@Override

publicvoid onCreate() {

super.onCreate();

Log.i("test","onCreate() executed !");

}

@Override

publicvoid onDestroy() {

super.onDestroy();

Log.i("test","onDestroy() executed !");

}

@Override

publicboolean onUnbind(Intent intent) {

Log.i("test","onUnbind executed !");

returnsuper.onUnbind(intent);

}

@Override

publicIBinder onBind(Intent intent) {

Log.i("test","onBind() executed !");

returnmyBinder;

}

class MyBinder extends Binder{

publicvoid startDownload(){

Log.i("test","MyBinder中的startDownload() executed !");

// 执行具体的下载任务,需开启一个子线程,在其中执行具体代码

}

}

}

MainActivity.java 的代码如下:

publicclass MainActivity extends AppCompatActivity implementsView.OnClickListener{

Button btnBind,btnUnBind;

MyService.MyBinder myBinder ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnBind = (Button) findViewById(R.id.bind);

btnUnBind = (Button) findViewById(R.id.btn_unBind);

btnBind.setOnClickListener(this);

btnUnBind.setOnClickListener(this);

}

ServiceConnection mServiceConnection = new ServiceConnection() {

@Override

publicvoid onServiceConnected(ComponentName componentName, IBinder iBinder) {

// 将IBinder向下转型为我们的内部类MyBinder

myBinder = (MyService.MyBinder) iBinder;

// 执行下载任务

myBinder.startDownload();

}

@Override

publicvoid onServiceDisconnected(ComponentName componentName) {

}

};

@Override

publicvoid onClick(Viewview) {

Intent mIntent = new Intent(MainActivity.this,MyService.class);

switch (view.getId()){

caseR.id.bind:

// 绑定Service

bindService(mIntent,mServiceConnection,BIND_AUTO_CREATE);

break;

caseR.id.btn_unBind:

// 取消绑定Service

unbindService(mServiceConnection);

break;

}

}

}

点击绑定按钮;

a5f456fc243f863b9d5f6bb6d4456a38.png

点击取消绑定按钮:

4de93b37755bf61d038db034e59f7acc.png

注意,如果我们没有先点击绑定,而是直接点击的取消绑定,程序会直接crash,报以下错误:

java.lang.IllegalArgumentException: Servicenotregistered: com.qc.admin.myserializableparceabledemo.MainActivity$1@8860e28

atandroid.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1120)

atandroid.app.ContextImpl.unbindService(ContextImpl.java:1494)

atandroid.content.ContextWrapper.unbindService(ContextWrapper.java:616)

atcom.qc.admin.myserializableparceabledemo.MainActivity.onClick(MainActivity.java:71)

75c275917a05e6647d4be6ab8a7dd247.png

细心的你也许早就发现了,Log中并没有打印"onServiceDisconnected executed !"这句,也就是说没有调用onServiceDisconnected()方法?从字面理解,onServiceConnected()方法是在Service建立连接的时候调用的,onServiceDisconnected()不就应该是在Service断开连接的时候调用的吗?其实不然,我们查看该方法的文档就知道了:

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder) when the Service is next running.

意思就是:当绑定到该Service的连接丢失的时候,该方法会被调用,典型的情况就是持有该Service的进程crash掉了,或者被杀死了。但是这并不会移除ServiceConnection 自身--它仍然是保持活跃状态,当Service下次被执行的时候,onServiceConnected(ComponentName, IBinder) 方法仍然会被调用。

但是要注意,如果我们按照刚才说的,不是先点击 bindService()方法,而是直接点击unbindService()方法,程序虽然也是crash掉了,但onServiceDisconnected()方法并不会被调用,这个很容易理解,毕竟都没有建立连接呢,谈何断开连接啊。但是如果我们已经绑定了Service,然后在后台直接终止该Service呢?结果会怎样?答案是onServiceDisconnected()方法仍然不会调用。这里我觉得应该是只有在意外的情况下进程结束,是由系统自动调用的,而非我们手动停止的。我们可以查看该方法内部的注释:

This is called when the connection with the service has been

unexpectedly disconnected -- that is, its process crashed.Because it

is running in our same process, we should never see this happen.

这段文字清楚的说明了该方法执行的场景:异常情况下导致断开了连接。也就是进程crash掉了。因为它运行在我们应用程序所在的进程中,因而我们将永远不希望看到这种情况发生。

Context.startService()和Context.bindService()同时使用

这两种方式是可以同时使用的,但是要注意,startService()和stopService()方法是对应的,而bindService()和unBind()方法是对应的,也就是说如果我们先调用startService()之后调用bindService()方法,或者相反,那么我们如果只调用stopService()或者只调用bindService()都无法停止该Service,只有同时调用才可以。

下面来看下具体代码:

MainActivity.java

publicclass MainActivity extends AppCompatActivity implementsView.OnClickListener{

Button btnStart,btnStop,btnBind,btnUnBind;

MyService.MyBinder myBinder ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnStart = (Button) findViewById(R.id.btn_start);

btnStop = (Button) findViewById(R.id.btn_stop);

btnBind = (Button) findViewById(R.id.btn_bind);

btnUnBind = (Button) findViewById(R.id.btn_unBind);

btnStart.setOnClickListener(this);

btnStop.setOnClickListener(this);

btnBind.setOnClickListener(this);

btnUnBind.setOnClickListener(this);

}

ServiceConnection mServiceConnection = new ServiceConnection() {

@Override

publicvoid onServiceConnected(ComponentName componentName, IBinder iBinder) {

// 将IBinder向下转型为我们的内部类MyBinder

myBinder = (MyService.MyBinder) iBinder;

// 执行下载任务

myBinder.startDownload();

}

@Override

publicvoid onServiceDisconnected(ComponentName componentName) {

Log.i("test","onServiceDisconnected executed !");

}

};

@Override

publicvoid onClick(Viewview) {

Intent mIntent = new Intent(MainActivity.this,MyService.class);

switch (view.getId()){

caseR.id.btn_start:

// 启动Service

startService(mIntent);

break;

caseR.id.btn_stop:

// 终止Service

stopService(mIntent);

break;

caseR.id.btn_bind:

// 绑定Service

bindService(mIntent,mServiceConnection,BIND_AUTO_CREATE);

break;

caseR.id.btn_unBind:

// 取消绑定Service

unbindService(mServiceConnection);

break;

}

}

}

MyService.java的代码:

publicclass MyService extends Service {

private MyBinder myBinder = new MyBinder();

publicMyService() {

}

@Override

publicvoid onCreate() {

super.onCreate();

Log.i("test","onCreate() executed !");

}

@Override

publicintonStartCommand(Intent intent,intflags,intstartId) {

Log.i("test","onStartComand() executed !");

returnsuper.onStartCommand(intent, flags, startId);

}

@Override

publicvoid onDestroy() {

super.onDestroy();

Log.i("test","onDestroy() executed !");

}

@Override

publicboolean onUnbind(Intent intent) {

Log.i("test","onUnbind executed !");

returnsuper.onUnbind(intent);

}

@Override

publicIBinder onBind(Intent intent) {

Log.i("test","onBind() executed !");

returnmyBinder;

}

class MyBinder extends Binder{

publicvoid startDownload(){

Log.i("test","MyBinder中的startDownload() executed !");

// 执行具体的下载任务

}

}

}

a.下面是依次点击start、bind、stop、unBind 按钮的输出结果:

5ad341023c79b8983e93e3b4ae759c13.png

b.下面是依次点击start、bind、unbind、stop 按钮时的输出结果:

13fd6b30740ca231b13addd6b1da55f9.png

在前台运行服务

我们上面一直说Service一般是用来在后台执行耗时操作,但是要知道,Service也是可以运行在前台的。后台Service的优先级比较低,容在内存不足等情况下被系统杀死,通过将其设置为前台,可以大大降低其被杀死的机会。前台Service会在系统通知栏显示一个图标,我们可以在这里进行一些操作。前台Service比较常见的场景有音乐播放器和天气预报等:

f490b1b0fff12703c21ec3f6549e8773.png

那么接下来我们就直接上代码:

@Override

publicvoid onCreate() {

super.onCreate();

Log.i("test","onCreate() executed !");

Intent mIntent = new Intent(this, SecondActivity.class);

PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

Notification mNotification = new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("My Notification ")

.setContentText("Hello World ! ")

.setContentIntent(mPendingIntent)

.build();

// 注意:提供给 startForeground() 的整型 ID 不得为 0。

// 要从前台移除服务,请调用 stopForeground()。此方法采用一个布尔值,指示是否也移除状态栏通知。

// 然而stopForeground()不会停止服务。 但是,如果您在服务正在前台运行时将其停止,则通知也会被移除。

startForeground(1, mNotification);

}

其实这里的实现很简单,就是将一个Notification通过startForeground(1, mNotification);传进去,从而将Notification与 Service建立起关联。我们点击这个通知,就会跳转到第二个Activity(但是该Notification并不会消失),截图如下:

7bfc2980742a566134dd1463a5519809.png

【编辑推荐】

【责任编辑:枯木 TEL:(010)68476606】

点赞 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值