{"id":23897982,"url":"https://github.com/byteszero/intentservicedemo","last_synced_at":"2026-06-13T01:31:46.338Z","repository":{"id":35666112,"uuid":"39941303","full_name":"BytesZero/IntentServiceDemo","owner":"BytesZero","description":"IntentServiceDemo","archived":false,"fork":false,"pushed_at":"2015-07-30T11:11:58.000Z","size":516,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-04T17:19:21.332Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BytesZero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-30T09:01:39.000Z","updated_at":"2021-04-02T02:42:54.000Z","dependencies_parsed_at":"2022-09-11T05:20:51.578Z","dependency_job_id":null,"html_url":"https://github.com/BytesZero/IntentServiceDemo","commit_stats":null,"previous_names":["byteszero/intentservicedemo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FIntentServiceDemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FIntentServiceDemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FIntentServiceDemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FIntentServiceDemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BytesZero","download_url":"https://codeload.github.com/BytesZero/IntentServiceDemo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240286506,"owners_count":19777354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-04T17:19:24.685Z","updated_at":"2025-11-14T01:02:08.506Z","avatar_url":"https://github.com/BytesZero.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IntentServiceDemo\n\n\u003e启动\n```java\nIntent intent=new Intent(context,UploadImgService.class);\nintent.setAction(ACTION_UPLOAD_IMG);\nintent.putExtra(EXTRA_IMG_PATH, path);\ncontext.startService(intent);\n```\n\u003e在实现的```onHandleIntent```方法中编写耗时操作\n\n```java\n@Override\nprotected void onHandleIntent(Intent intent) {\n    if (intent != null) {\n        String action = intent.getAction();\n        if (ACTION_UPLOAD_IMG.equals(action)) {\n            String path=intent.getStringExtra(EXTRA_IMG_PATH);\n\n            try {\n                Thread.sleep(2000);\n                Logger.e(\"onHandleIntent\" + path);\n                EventBus.getDefault().post(new MessageEvent(path));\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n\n    }\n}\n```\n\u003e完整代码\n\n```java\n\n/**\n * Created by zsl on 15/7/30.\n */\npublic class UploadImgService extends IntentService {\n    private static final String ACTION_UPLOAD_IMG = \"com.zsl.intentservicedemo.UploadImgService.ActionUploadImg\";\n    private static final String EXTRA_IMG_PATH = \"com.zsl.intentservicedemo.UploadImgService.ExtraImgPath\";\n\n    /**\n     * 启动SerVice\n     *\n     * @param context\n     * @param path\n     */\n    public static void startUPloadImg(Context context, String path) {\n        Intent intent=new Intent(context,UploadImgService.class);\n        intent.setAction(ACTION_UPLOAD_IMG);\n        intent.putExtra(EXTRA_IMG_PATH, path);\n        context.startService(intent);\n    }\n    public UploadImgService() {\n        super(\"UploadImgService\");\n    }\n\n    @Override\n    protected void onHandleIntent(Intent intent) {\n        if (intent != null) {\n            String action = intent.getAction();\n            if (ACTION_UPLOAD_IMG.equals(action)) {\n                String path=intent.getStringExtra(EXTRA_IMG_PATH);\n\n                try {\n                    Thread.sleep(2000);\n                    Logger.e(\"onHandleIntent\" + path);\n                    EventBus.getDefault().post(new MessageEvent(path));\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n\n        }\n    }\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        Logger.e(\"UploadImgService:onCreate\");\n    }\n\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n        Logger.e(\"UploadImgService:onDestroy\");\n    }\n}\n```\n\n\u003e注意事项\n\n- ```IntentService```也是```Service```所以必须在清单文件中注册，注册方法就是```Service```\n- 结合```EventBus```使用时，要在```public void onEventMainThread(MessageEvent event)```中更新UI\n\n![图片](device-2015-07-30-190954.png)\n\n[视频](device-2015-07-30-185450.mp4)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyteszero%2Fintentservicedemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyteszero%2Fintentservicedemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyteszero%2Fintentservicedemo/lists"}