IntentService

本文详细介绍了Android中的IntentService组件,解释了其应用场景、工作原理及如何使用。通过示例代码展示了IntentService如何处理多个异步请求,并自动管理自身的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Service的应用场景:在后台只处理一个请求时比较合适

IntentService的应用场景:service需要处理多个请求,当然处理多个请求是一个比较危险的多线程的场景

1.什么是IntentService

简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
而且,所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。

示例代码:

TestService3.java

publicclass IntentServiceDemo extends IntentService {  
public IntentServiceDemo() {  
//必须实现父类的构造方法
super("IntentServiceDemo");  
   }  

//重写其他方法,用于查看方法的调用顺序 
@Override
public IBinder onBind(Intent intent) {  
       System.out.println("onBind");  
returnsuper.onBind(intent);  
   }  

@Override
publicvoid onCreate() {  
       System.out.println("onCreate");  
super.onCreate();  
   }  

@Override
publicvoid onStart(Intent intent, int startId) {  
       System.out.println("onStart");  
super.onStart(intent, startId);  
   }

@Override
publicint onStartCommand(Intent intent, int flags, int startId) {  
       System.out.println("onStartCommand");  
returnsuper.onStartCommand(intent, flags, startId);  
   }

@Override
publicvoid setIntentRedelivery(boolean enabled) {  
super.setIntentRedelivery(enabled);  
       System.out.println("setIntentRedelivery");  
   }

 //必须重写的核心方法  
@Override
protectedvoid onHandleIntent(Intent intent) {  
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
       String action = intent.getExtras().getString("param");  
if (action.equals("oper1")) {  
           System.out.println("Operation1");  
       }elseif (action.equals("oper2")) {  
           System.out.println("Operation2");  
       }  
try {  
           Thread.sleep(2000);  //让服务休眠2秒  
       } catch (InterruptedException e) {  
           e.printStackTrace();  
       }  
   }  
@Override
publicvoid onDestroy() {  
       System.out.println("onDestroy");  
super.onDestroy();  
   }  
}  

AndroidManifest.xml注册下Service

<service android:name=".TestService3" android:exported="false">  
    <intent-filter >  
        <action android:name="com.test.intentservice"/>  
    </intent-filter>  
</service>  

在MainActivity启动三次服务

public class MainActivity extends Activity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        Intent it1 = new Intent("com.test.intentservice");  
        Bundle b1 = new Bundle();  
        b1.putString("param", "s1");  
        it1.putExtras(b1);  

        Intent it2 = new Intent("com.test.intentservice");  
        Bundle b2 = new Bundle();  
        b2.putString("param", "s2");  
        it2.putExtras(b2);  

        Intent it3 = new Intent("com.test.intentservice");  
        Bundle b3 = new Bundle();  
        b3.putString("param", "s3");  
        it3.putExtras(b3);  

        //接着启动多次IntentService,每次启动,都会新建一个工作线程  
        //但始终只有一个IntentService实例  
        startService(it1);  
        startService(it2);  
        startService(it3);  
    }  
} 

打印结果如图:
三个异步任务依次排队操作,任务完成后,service自动停止服务。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值