2

アプリのIntentServiceを使用して、アプリケーションアクティビティを妨害したり妨害したりせずに、バックグラウンドスレッドでサーバーからデータを取得したいと考えています。これが私の単純なIntentServiceです

public class SearchService extends IntentService  {

    public SearchService() {
    super("SearchService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    Log.d("Tag","service started");
    }

}

アプリケーションのメインアクティビティでこのサービスを開始します

public class ChannelsActivity extends Activity {
   @Override
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            Intent intent = new Intent(this , SearchService.class);
            startService(intent);   
         }

マニフェストファイルでサービス属性も定義していますが、機能しません。何が問題なのかわかりません。

 <application
    <activity
        android:name=".ChannelsActivity"
        android:screenOrientation="landscape"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".SearchService"/>

</application>

私はこの問題で立ち往生しています。どんな助けでも私にとって本当に役に立ちます。

4

1 に答える 1

0

投稿したコードは機能するはずです。logcat ウィンドウに「service started」というデバッグ ログが表示されますか? ばかげているように聞こえますが、SearchService は実行され、すぐにシャットダウンします。これは、実行する大規模な作業がなく、それが目標である場合、実行中のサービスにサービスが表示されないためです。

それが役に立てば幸い。

于 2012-10-22T18:20:23.877 に答える