一起Talk Android吧(第二百零一回:Android中的IntentService四)

本文深入讲解了Android中IntentService的另一种启动方式——helper method。通过具体示例,介绍了如何使用startActionFoo等静态方法启动IntentService,并解析了其工作原理及优势。

各位看官们大家好,上一回中咱们说的是Android中IntentService的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,上一回中我们用具体的代码演示了如何使用IntentService,这一回中我们将介绍另外一种启动IntentService的方法:helper method。这也是官方推荐使用的方法,其实我们使用AST创建IntentService时默认会创建两个helper方法:startActionFoostartActionBaz

这些方法是静态方法,用来启动IntentService.它们有三个参数:第一个参数是context类型,用来调用startService; 在Activity中调用此方法时传入Activity的context就可以;剩下两个参数是String类型,用来当作intent的数据,在onHandleIntent方法中可以从intent中解析出这些数据;

这两种方法使用了不同的ACTION,以便区分不同的方法。我们这里使用了startActionFoo方法来启动IntentService.具体操作如下:

  • 1.IntentService以及Activyt的代码复用上一回的代码;
  • 2.在Activity中添加一个Buttone用来启动IntentService;
<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" />
    
  • 3.给步骤2中的Buttion添加监听器,监听器中IntentService的静态方法startActionFoo方法来启动IntentService;
 //通过某种Action:Foo来启动IntentService
mButtonStartIntentServiceByFooAction = (Button)findViewById(R.id.id_foo_intentservice);
mButtonStartIntentServiceByFooAction.setOnClickListener(v ->
        IntentServiceA.startActionFoo(this,"Foo Action",null));
  • 4.在IntentService中响应startActionFoo,简单起见,使用log打印参数内容.
public static void startActionFoo(Context context, String param1, String param2) {
    Log.i(TAG, "startActionFoo: ");
    Intent intent = new Intent(context, IntentServiceA.class);
    intent.setAction(ACTION_FOO);
    intent.putExtra(EXTRA_PARAM1, param1);
    intent.putExtra(EXTRA_PARAM2, param2);
    context.startService(intent);
}
    @Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "onHandleIntent: ThreadID: "+Thread.currentThread().getId());
    Log.i(TAG, "onHandleIntent: ThreadName: "+Thread.currentThread().toString());
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_FOO.equals(action)) {
            final String param1 = intent.getStringExtra(EXTRA_PARAM1);
            final String param2 = intent.getStringExtra(EXTRA_PARAM2);
            handleActionFoo(param1, param2);
        } else if (ACTION_BAZ.equals(action)) {
            final String param1 = intent.getStringExtra(EXTRA_PARAM1);
            final String param2 = intent.getStringExtra(EXTRA_PARAM2);
            handleActionBaz(param1, param2);
        }
    }
}
private void handleActionFoo(String param1, String param2) {
    // TODO: Handle action Foo
    if(param1 != null) {
        Log.i(TAG, "handleActionFoo: param1: " + param1);
    }else {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

下面是程序的运行结果,请大家参考:

 //按下IntentService by foo Action会启动IntentService
 I/IntentServiceA: startActionFoo: 
 I/IntentServiceA: onCreate: 
 I/IntentServiceA: onCreate: ThreadID: 2
 I/IntentServiceA: onCreate: ThreadName: Thread[main,5,main]
 I/IntentServiceA: onStartCommand: 
 
 //IntentService启动后运行异步操作
 I/IntentServiceA: onHandleIntent: ThreadID: 1132
 I/IntentServiceA: onHandleIntent: ThreadName: Thread[IntentService[IntentServiceA],5,main]

 //在IntentService中响应startActionFoo,这里只打印一行log
 I/IntentServiceA: handleActionFoo: param1: Foo Action
 
 //IntentService后运行完成后自动销毁
 I/IntentServiceA: onDestroy:
 

看官们对比一下上一章回中我们在Activity中直接使用startService启动IntentService,这种helper方法可以隐藏IntentService的细节,启动IntentService时只需要传递给某种数据给它就可以。因此在实际工程中建议大家使用这种方法来启动IntentService.

各位看官,关于Android中IntentService的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值