IntentService的使用

Service的启动方式
1)startService启动。启动的Service就一直(相对来说)在后台允许,与启动该service的其他组件的生命周期没有关系。
2)bindService启动。如果是Activity启动的,那么当这个Activity销毁的时候Service也销毁。

  假设我们有这样的需求:我们想让一个Service帮我们干一个比较耗时的工作(比如下载大文件),当Service干完活后自动销毁(目的是为节省内存)。

则可以采用IntentService进行处理

IntentServie的使用 Demo代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:onClick="startIntentService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动IntentService" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.intentservicedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.IntentServiceDemo">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyIntentService"/>
    </application>

</manifest>

MainActivity.java

package com.test.intentservicedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void startIntentService(View view) {
        Intent intent = new Intent(this, MyIntentService.class);
        intent.putExtra("url", "https://www.baidu.com/****");
        startService(intent);
    }
}

MyIntentService.java

package com.test.intentservicedemo;

import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.Nullable;


public class MyIntentService extends IntentService {
    //必须要添加,不然启动不了
    public MyIntentService() {
        this("MyThreadName");
    }
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     * name:用于处理onHandleIntent方法的线程的名称
     */
    public MyIntentService(String name) {
        super(name);
    }

    //用于处理Intent的请求,该方法是在子线程中执行。给这个子线程起个名称
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d("abc", "ThreadName = " + Thread.currentThread().getName());
        String url = intent.getStringExtra("url");
        Log.d("abc", "url:" + url);

        // 模拟耗时操作
        SystemClock.sleep(5000);

        Log.d("abc", "执行完任务了");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("abc", "onDestroy");
    }
}

运行结果:

2021-05-27 09:16:48.675 18833-18882/com.test.intentservicedemo D/abc: ThreadName = IntentService[MyThreadName]
2021-05-27 09:16:48.675 18833-18882/com.test.intentservicedemo D/abc: url:https://www.baidu.com/****
2021-05-27 09:16:53.676 18833-18882/com.test.intentservicedemo D/abc: 执行完任务了
2021-05-27 09:16:53.679 18833-18833/com.test.intentservicedemo D/abc: onDestroy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值