サービスの終了操作を気にしないでいいようです。
5秒後にサービス終了するコード。
package com.example.intentservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, HelloIntentService.class);
startService(intent);
}
}
package com.example.intentservice;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class HelloIntentService extends IntentService {
private static final String TAG = "HelloIntentService";
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public HelloIntentService() {
super("HelloIntentService");
Log.v(TAG, "HelloIntentService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG, "onHandleIntent");
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5 * 1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
Log.v(TAG, "onHandleIntent 終了");
}
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
}
}
UIの変更を考えてみる
package com.example.intentservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.widget.TextView;
public class MainActivity extends Activity {
public static class SampleHandler extends Handler {
private MainActivity con;
public SampleHandler(MainActivity mainActivity) {
// TODO 自動生成されたコンストラクター・スタブ
this.con = mainActivity;
}
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
TextView textView = (TextView) con.findViewById(R.id.textView1);
textView.setText( bundle.getString("mystring") );
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SampleHandler handler = new SampleHandler(this);
Intent intent = new Intent(this, HelloIntentService.class);
intent.putExtra("messenger", new Messenger(handler));
startService(intent);
}
}
package com.example.intentservice;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class HelloIntentService extends IntentService {
private static final String TAG = "HelloIntentService";
public HelloIntentService() {
super("HelloIntentService");
Log.v(TAG, "HelloIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG, "onHandleIntent");
Bundle bundle = intent.getExtras();
if (bundle != null) {
Messenger messenger = (Messenger) bundle.get("messenger");
Message msg = Message.obtain();
Bundle data = new Bundle();
data.putString("mystring", "Hello!");
msg.setData(data);
try {
messenger.send(msg);
} catch (RemoteException e) {
Log.i("error", "error");
}
}
}
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
}
}
while ループが永遠に終わらない処理の場合、
呼び出し元のアクティビティーが終了しても、動き続けるので、
IntentServiceを終了させる処理が必要なようです。
package com.example.intentservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.widget.TextView;
public class MainActivity extends Activity {
public static class SampleHandler extends Handler {
private MainActivity con;
public SampleHandler(MainActivity mainActivity) {
// TODO 自動生成されたコンストラクター・スタブ
this.con = mainActivity;
}
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
TextView textView = (TextView) con.findViewById(R.id.textView1);
textView.setText(bundle.getString("mystring"));
}
}
private SampleHandler handler;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new SampleHandler(this);
intent = new Intent(this, HelloIntentService.class);
intent.putExtra("messenger", new Messenger(handler));
startService(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
// The service is no longer used and is being destroyed
if (intent != null) {
stopService(intent);
intent = null;
}
}
}
package com.example.intentservice;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class HelloIntentService extends IntentService {
private static final String TAG = "HelloIntentService";
private boolean loopFlag = true;
public HelloIntentService() {
super("HelloIntentService");
Log.v(TAG, "HelloIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG, "onHandleIntent");
Bundle bundle = intent.getExtras();
while (loopFlag) {
synchronized (this) {
try {
Log.v(TAG, "HelloIntentService");
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"));
String stringTime = gc.get(Calendar.HOUR_OF_DAY) + ":" + gc.get(Calendar.MINUTE) + ":" + gc.get(Calendar.SECOND);
if (bundle != null) {
Messenger messenger = (Messenger) bundle.get("messenger");
Message msg = Message.obtain();
Bundle data = new Bundle();
data.putString("mystring", stringTime);
msg.setData(data);
try {
messenger.send(msg);
} catch (RemoteException e) {
Log.i("error", "error");
}
}
wait(1000);
} catch (Exception e) {
}
}
}
}
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
loopFlag = false;
}
}

0 件のコメント:
コメントを投稿