Android
IntentService
サーチ…
構文
- <service android:name = "。UploadS3IntentService" android:exported = "false" />
備考
IntentServiceは、バックグラウンドスレッドで作業をオフロードする簡単な方法を提供します。リクエストを受信し、キューに入れたり、自分自身を停止したりするなど、すべての処理を行います。実装が簡単で、メイン(UI)スレッドに属さない操作に時間がかかる場合に使用するのに最適です。
IntentServiceの作成
、IntentServiceを作成し拡張するクラスを作成するためにIntentService 、オーバーライドする方法、及びその中onHandleIntent :
package com.example.myapp;
public class MyIntentService extends IntentService {
@Override
protected void onHandleIntent (Intent workIntent) {
//Do something in the background, based on the contents of workIntent.
}
}
サンプルインテントサービス
ここでは、バックグラウンドで画像をロードIntentServiceうとするIntentService例を示します。 IntentServiceを実装するために必要なのは、 super(String)コンストラクタを呼び出すコンストラクタを提供することだけで、 onHandleIntent(Intent)メソッドを実装する必要があります。
public class ImageLoaderIntentService extends IntentService {
public static final String IMAGE_URL = "url";
/**
* Define a constructor and call the super(String) constructor, in order to name the worker
* thread - this is important if you want to debug and know the name of the thread upon
* which this Service is operating its jobs.
*/
public ImageLoaderIntentService() {
super("Example");
}
@Override
protected void onHandleIntent(Intent intent) {
// This is where you do all your logic - this code is executed on a background thread
String imageUrl = intent.getStringExtra(IMAGE_URL);
if (!TextUtils.isEmpty(imageUrl)) {
Drawable image = HttpUtils.loadImage(imageUrl); // HttpUtils is made-up for the example
}
// Send your drawable back to the UI now, so that you can use it - there are many ways
// to achieve this, but they are out of reach for this example
}
}
IntentServiceを開始するには、 IntentをIntentServiceに送信する必要があります。 Activityから行うことができます。もちろん、あなたはそれに限定されません。ここでは、新しいServiceをActivityクラスから呼び出す方法の例を示します。
Intent serviceIntent = new Intent(this, ImageLoaderIntentService.class); // you can use 'this' as the first parameter if your class is a Context (i.e. an Activity, another Service, etc.), otherwise, supply the context differently
serviceIntent.putExtra(IMAGE_URL, "http://www.example-site.org/some/path/to/an/image");
startService(serviceIntent); // if you are not using 'this' in the first line, you also have to put the call to the Context object before startService(Intent) here
IntentServiceはIntentからのデータをIntentService処理するため、複数のIntentを互いに衝突させる心配なしに送信できます。一度に1つのIntentのみが処理され、残りはキューに入れられます。すべてのジョブが完了すると、 IntentServiceは自動的にシャットダウンします。
基本的なIntentServiceの例
抽象クラスIntentServiceは、サービスの基本クラスであり、ユーザーインターフェイスなしでバックグラウンドで実行されます。したがって、UIを更新するには、受信者を使用する必要があります。受信者は、 BroadcastReceiverまたはResultReceiverいずれかResultReceiver 。
- サービスが通信をリッスンしたい複数のコンポーネントと通信する必要がある場合は、
BroadcastReceiverを使用する必要があります。 -
ResultReceiver:あなたのサービスが親アプリケーション(つまりあなたのアプリケーション)だけと通信する必要がある場合に使用します。
IntentService内には、 onHandleIntent()重要なメソッドが1つあります。このメソッドでは、通知の準備、アラームの作成など、すべての操作を行います。
あなた自身のIntentServiceを使いたい場合は、次のように拡張する必要があります:
public class YourIntentService extends IntentService {
public YourIntentService () {
super("YourIntentService ");
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO: Write your own code here.
}
}
アクティビティの呼び出し/開始は、次のように行うことができます。
Intent i = new Intent(this, YourIntentService.class);
startService(i); // For the service.
startActivity(i); // For the activity; ignore this for now.
任意のアクティビティと同様に、バンドルデータなどの追加情報を次のように渡すことができます。
Intent passDataIntent = new Intent(this, YourIntentService.class);
msgIntent.putExtra("foo","bar");
startService(passDataIntent);
ここで、いくつかのデータをYourIntentServiceクラスに渡したとします。このデータに基づいて、次のようにアクションを実行できます。
public class YourIntentService extends IntentService {
private String actvityValue="bar";
String retrivedValue=intent.getStringExtra("foo");
public YourIntentService () {
super("YourIntentService ");
}
@Override
protected void onHandleIntent(Intent intent) {
if(retrivedValue.equals(actvityValue)){
// Send the notification to foo.
} else {
// Retrieving data failed.
}
}
}
上記のコードでは、 OnHandleIntent()メソッドで制約を処理する方法も示しています。