Android服务一 创建启动服务
生活随笔
收集整理的這篇文章主要介紹了
Android服务一 创建启动服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
若要學習創建綁定服務,請查看下篇Android服務二 創建綁定服務
啟動服務
基于Service
package service;import android.app.Service; import android.content.Intent; import android.os.IBinder;/*** 創建啟動服務*/ public class MyService extends Service {@Overridepublic void onCreate() {}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return 0;}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {}} String address = "https://img-blog.csdn.net/20160527205804527"; Intent intent = new Intent(this, MyService.class); intent.putExtra("address", address); startService(intent);基于IntentService
package service;import android.app.IntentService; import android.content.Intent;import HttpURL.HttpUtil;/*** 使用IntentService創建啟動服務*/ public class StartIntentService extends IntentService {public StartIntentService() {super("Download Picture");}@Overrideprotected void onHandleIntent(Intent intent) {HttpUtil.downloadPicture(intent.getStringExtra("address"));} } String address = "https://img-blog.csdn.net/20160527205804527"; Intent intent = new Intent(this, MyService.class); intent.putExtra("address", address); startService(intent);總結
以上是生活随笔為你收集整理的Android服务一 创建启动服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Picasso加载网页图片到Recy
- 下一篇: Android服务二 创建绑定服务