1

Im sure this is a very common problem but have not seen any questions asked on how to accomplish the following.

I created a service that runs every 5 minutes and gets called when the device has finished on BOOT_COMPLETE and this works just fine.

The problem I am encountering is when I am debugging on my device I have to re-boot my phone in order to get the service to launch. Is there a way for me to start this background service that runs every 5 mintues on Application Startup.

So what I need is to check if the service is running in the first place and if it is not start it.

Just to elaborate a little bit more, this service fetches data of the web so my ideal situation of having it get kicked off by BOOT_COMPLETE works just great. But in the scenarios that the user downloads the app and launches it this service will not get kicked off so I need a manual way of starting. If the service is not running, which it wouldn't be if the user just installed it I need to start it.

4

3 に答える 3

2

次のリンクで使用されているアプローチを使用して、実際に問題を解決しました。

ブロードキャスト メッセージの送受信方法

マニフェストから IntentFilter を削除し、手動で呼び出します。これが他の誰かに役立つことを願っています。

    if(!isMyServiceRunning()) {
        Intent intent = new Intent(mContext, OnBootReceiver.class);
        sendBroadcast(intent); 
    }
于 2013-06-11T21:14:07.730 に答える
1

を使用して、サービスを手動で開始できますadb shelladb シェルから Android サービスを開始および停止する方法を参照してください。

コマンドは次のとおりです。

am startservice com.your.package/.ServiceClassName

Android のバージョンによっては、有効な構文が若干異なる場合があります。次に例を示します。

am startservice -n com.your.package/.ServiceClassName
am startservice -a com.your.package/.ServiceClassName
am startservice --user 0 -n com.your.package/.ServiceClassName
于 2013-06-11T20:26:41.323 に答える
1

BOOT_COMPLETE イベントによって開始されたスティッキー サービスを実行したいようです

http://developer.android.com/reference/android/app/Service.html#START_STICKY

開発の場合は、別のインテント フィルターをサービスに追加してサービスを開始できるため、コードを更新するたびに、デバイスを再起動することなく、コマンド ラインからアクティビティ マネージャーを介して手動でトリガーできます。

于 2013-06-11T20:26:50.637 に答える