Re: [問題] Service在開機時啟動

看板AndroidDev作者 (N=NP)時間13年前 (2011/06/23 14:26), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串2/3 (看更多)
Sorry, I cannot type Chinese now. But, here I can start Service easily here Actually, It's just need to understand INTENT And there are just three step if you want to start Service at booting Step 1. In AndroidManifest.xml, add Intent filter for BOOT_COMPLETED example. ----------------------------------------- <application ...> <service ... > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </service> </application> ------------------------------------- Step 2. In AndroidManifest.xml open the uses-permission about BOOT_COMPLETED example. ---------------------------------- <application ...> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -------------------------------------- Step 3. print Log to makesure the Service.onCreate() is invoked at booting example. -------------------------------- //In XxxxService.java public void onCreate() { super.onCreate(); Log.i("SERVICE_WHEN_BOOT", "XxxxService onCreate() is invoked" ); ... } ----------------------------------- please reboot after install the apk.:) To open your eclipse DDMS and add a filter of SERVICE_WHEN_BOOT Then reboot after installing the .apk. You will see that the string "XxxxService onCreate() is invoked" appears in SERVICE_WHEN_BOOT filter of eclipse DDMS Enjoy it :) ※ 引述《sicao (adios)》之銘言: : 標題: [問題] Service在開機時啟動 : 時間: Mon Jun 20 20:44:12 2011 : : 最近想嘗試做這東西 : 但是參考了底下這些範例 : : http://www.androidenea.com/2009/09/starting-android-service-after-boot.html : http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ : http://blog.gregfiumara.com/archives/82 : : 都沒有成功過.. : : 是範例有問題還是有我沒注意到的地方呢 : : -- : : 5L36f6d6I5207573K657273206f6E62066Y697265O666f78206172U65207375636b2e : : -- : ※ 發信站: 批踢踢實業坊(ptt.cc) : ◆ From: 111.240.42.43 : → tomap41017:反正只要在onReceive內啟動service就好囉!! 06/21 00:50 : → tomap41017:記得註冊Receiver以及Intent.RECEIVE_BOOT_COMPLETE 06/21 00:50 : 推 milochen:建議是先找到一個完整的Project能動,之後再參考這些. 06/21 02:48 : → milochen:因為先求能動,再參考些文章昀內容。會比較快理清問題. 06/21 02:50 : 推 jim31837:建議把你的專案丟上來 或至少丟logcat的訊息 06/21 04:52 : → sicao:問題就是找不到可以用的project.. 06/21 08:53 : → sicao:http://www.multiupload.com/SU1ZF9FPEO 這是我照網路上做的 06/21 08:53 : → tomap41017:我不懂你為何要執著於setAction,直接用Intent(context 06/21 14:05 : → tomap41017:, Service.class)不是很歡樂嗎? @@ 06/21 14:06 : 推 tomap41017:琣b猜這邊改一下應該會過@@ 06/21 14:08 : 推 tomap41017:你的project在開機完成後就出現Exception囉 06/21 14:10 : → sicao:http://www.multiupload.com/BV7EPIBUKV 改這樣還是掛.. 06/21 18:53 : → sicao:http://www.multiupload.com/J46JBEBEWI 這個可以跑.. 06/21 18:54 : → sicao:但是一退出就沒有在Running Service清單看到它?? 06/21 18:54 : → tomap41017:service.onStarcCommand()return START_STICKY 06/21 19:30 : 推 jim31837:你到StackOverflow問看看 直覺上應該有這類的問題 06/22 00:51 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 115.81.178.163 ※ 編輯: milochen 來自: 115.81.178.163 (06/23 14:32)

06/23 15:05, , 1F
Thx~I will try it..
06/23 15:05, 1F

06/23 19:18, , 2F
推!!本來才想寫教學的XDD
06/23 19:18, 2F

10/27 09:41, , 3F
大推!!!!
10/27 09:41, 3F
文章代碼(AID): #1E0jo63H (AndroidDev)
文章代碼(AID): #1E0jo63H (AndroidDev)