5.23

所花时间(包括上课):1

打码量(行):100

博客量(篇):1

了解到知识点:学习IntentService

 

package com.example.myapp;

 

import android.app.IntentService;

import android.content.Intent;

import android.util.Log;

 

public class MyIntentService extends IntentService {

 

    private static final String TAG = "MyIntentService";

 

    public MyIntentService() {

        super("MyIntentService");

    }

 

    @Override

    protected void onHandleIntent(Intent intent) {

        // 模拟一个耗时任务

        Log.d(TAG, "IntentService started");

 

        // 从 Intent 中获取额外的数据

        if (intent != null) {

            String data = intent.getStringExtra("data");

            Log.d(TAG, "Data received: " + data);

        }

 

        // 模拟任务执行结束

        try {

            Thread.sleep(3000); // 模拟耗时操作,例如下载或处理数据

        } catch (InterruptedException e) {

            Thread.currentThread().interrupt();

        }

 

        // 任务完成后停止服务

        Log.d(TAG, "IntentService finished");

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        Log.d(TAG, "IntentService destroyed");

    }

}

posted @ 2024-05-23 20:29  赵千万  阅读(1)  评论(0编辑  收藏  举报