各位看官们大家好,上一回中咱们说的是Android中IntentService的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!
看官们,我们在上一章回中介绍了IntentService的使用步骤,在本章回中我们将通过代码结合文字的形式来演示如何使用它,下面是具体的步骤,请大家参考:
- 1.复用前面章回中Service的工程,在工程中创建IntentService的子类IntentServiceA;
public class IntentServiceA extends IntentService {
}
- 2.重写IntentServiceA中的回调方法,特别是回调方法:onHandleIntent;
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent: ThreadID: "+Thread.currentThread().getId());
Log.i(TAG, "onHandleIntent: ThreadName: "+Thread.currentThread().toString());
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
Log.i(TAG, "onCreate: ThreadID: "+Thread.currentThread().getId());
Log.i(TAG, "onCreate: ThreadName: "+Thread.currentThread().toString());
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
if (mBinder != null)
return mBinder;
else
return super.onBind(intent);
}
@Override
public int onStartCommand( Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy: ");
super.onDestroy();
}
- 3.自定义Binder的子类:IntentServiceBinderSub,并且在类中创建foo方法,该方法用来实现服务的相关操作,简单起见,我们只是打印一行log;
class IntentServiceBinderSub extends Binder {
public void foo() {
Log.i(TAG, "foo of IntentServiceBinderSub: ");
}
}
- 4.创建IntentServiceBinderSub对象,并且在onBind方法中返回该对象;
private IntentServiceBinderSub mBinder = new IntentServiceBinderSub();
- 5.创建ConnectionImpl类并且实现ServiceConnection接口;在类中重写onnServiceConnected和onServiceDisconnected方法;
class ConnectionImpl implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected: ");
//使用服务提供的func方法来实现Activity和服务通信
if(service instanceof ServiceA.BinderSub) {
mBinderSub = (ServiceA.BinderSub) service;
mBinderSub.func();
}else if (service instanceof IntentServiceA.IntentServiceBinderSub) {
mIntentServiceBinderSub =(IntentServiceA.IntentServiceBinderSub) service;
mIntentServiceBinderSub.foo();
}else {
Log.i(TAG, "onServiceConnected: There is not Binder");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected: ");
}
}
- 6.创建ConnectionImpl对象,以便在绑定和解绑定服务时使用,这里复用Service的代码就可以;
private ConnectionImpl mConnection;
- 7.在Activity布局中添加四个Button,用来操作服务:启动,停止,绑定,解绑各对应一个Button;
<Button
android:id="@+id/id_start_intentservice"
android:text="Start IntentService"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_stop_intentservice"
android:text="Stop IntentService"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_bind_intentservice"
android:text="Bind IntentService"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_unbind_intentservice"
android:text="UnBind IntentService"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_foo_intentservice"
android:text="IntentService by foo Action"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- 8.为步骤7中的Button添加事件监听器,并且在监听器中调用相应的方法来操作服务,这些方法和Service的方法完全相同。
//启动,停止,绑定,解除绑定IntentService的Buttion
mButtonStartIntentService = (Button)findViewById(R.id.id_start_intentservice);
mButtonStopIntentService = (Button)findViewById(R.id.id_stop_intentservice);
mButtonBindIntentService = (Button)findViewById(R.id.id_bind_intentservice);
mButtonUnBindIntentService = (Button)findViewById(R.id.id_unbind_intentservice);
//给操作Service的Button添加事件监听器
final Intent intent = new Intent(this,ServiceA.class);
mButtonStartService.setOnClickListener(v -> startService(intent));
mButtonStopService.setOnClickListener(v -> stopService(intent));
mButtonBindService.setOnClickListener(v -> bindService(intent,mConnection,this.BIND_AUTO_CREATE));
mButtonUnBindService.setOnClickListener(v -> unbindService(mConnection));
下面是程序的运行结果,请大家参考:
//按下startIntentService Buttion后启动IntentService,并且异步运行onHandleIntent
//我们从log中的线程ID就能看出来,异步操作完成后它会自动销毁
I/IntentServiceA: onCreate:
I/IntentServiceA: onCreate: ThreadID: 2
I/IntentServiceA: onCreate: ThreadName: Thread[main,5,main]
I/IntentServiceA: onStartCommand:
I/IntentServiceA: onHandleIntent: ThreadID: 1129
I/IntentServiceA: onHandleIntent: ThreadName: Thread[IntentService[IntentServiceA],5,main]
I/IntentServiceA: onDestroy:
//按下BindIntentService Buttion后启动IntentService,
//它没有异步运行,而是运行Binder子类中的方法:foo
I/IntentServiceA: onCreate:
I/IntentServiceA: onCreate: ThreadID: 2
I/IntentServiceA: onCreate: ThreadName: Thread[main,5,main]
I/IntentServiceA: onBind:
I/ServiceA: onServiceConnected:
I/IntentServiceA: foo of IntentServiceBinderSub:
//按下UnBindIntentService Buttion后销毁IntentService,
I/IntentServiceA: onUnbind:
I/IntentServiceA: onDestroy:
看官们,以上的步骤和普通服务的步骤完全相同,不同的地方有两个:
- 自定义的Binder子类;
- 回调方法onHandleIntent;
此外,我们在介绍service时是分启动和绑定两个场景介绍的,在这里把这两个场景合成了一个场景,因此步骤显的有些多。
各位看官,关于Android中IntentService的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
IntentService详解
本文详细讲解了Android中IntentService的使用,包括创建子类、重写回调方法、自定义Binder及其实现方法,以及如何在Activity中操作IntentService。
7496

被折叠的 条评论
为什么被折叠?



