ラベル Service の投稿を表示しています。 すべての投稿を表示
ラベル Service の投稿を表示しています。 すべての投稿を表示

2013年10月6日日曜日

IntentService を使ってみる:android

http://developer.android.com/guide/components/services.html で紹介されているソースコードを実行してみる。
サービスの終了操作を気にしないでいいようです。
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;
 }

}

2013年10月4日金曜日

サービスのバインドについて考えてみる


サービスをバインドして、サービスからUIを変更
package com.example.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;

import com.example.bindservice.SampleService.LocalBinder;

public class MainActivity extends Activity {

 private static final String TAB = "MainActivity";
 private TextView tv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.textView1);

 }

 @Override
 public void onStart() {
  Log.i(TAB, "onStart");
  super.onStart();
  // Bind
  Intent intent = new Intent(getApplicationContext(), SampleService.class);
  bindService(intent, mySrviceConnection, Context.BIND_AUTO_CREATE);//サービスをバインド
 }

 @Override
 public void onStop() {
  Log.i(TAB, "onStop");
  super.onStop();
  // Unbind
  unbindService(mySrviceConnection);//サービスをアンバインド
 }

 private ServiceConnection mySrviceConnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO 自動生成されたメソッド・スタブ
   LocalBinder binder = (SampleService.LocalBinder) service;
   binder.getService(tv);
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO 自動生成されたメソッド・スタブ

  }
 };

}
package com.example.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;

public class SampleService extends Service {

 private static final String TAB = "SampleService";
 private IBinder mBinder = new LocalBinder();

 public class LocalBinder extends Binder {
        SampleService getService(TextView tv) {
      Log.i(TAB, "LocalBinder");//
      tv.setText("サービスから変更");
            return SampleService.this;
        }
    }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  Log.i(TAB, "onBind");//起動時に呼ばれる

  return mBinder;
 }

 @Override
 public void onDestroy() {
  Log.i(TAB, "onDestroy");
 }
}

ボタンクリックでUIを変更してみる
package com.example.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.bindservice.SampleService.LocalBinder;

public class MainActivity extends Activity {

 private static final String TAB = "MainActivity";
 private TextView tv;
 private LocalBinder binder;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.textView1);

  Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Log.i(TAB, "ボタン2が押された");
    if (binder != null){
     Log.i(TAB, "binder は null で無い");
     binder.getService(tv);
    }else{
     Log.i(TAB, "binder は null");
    }
   }
  });
 }

 @Override
 public void onStart() {
  Log.i(TAB, "onStart");
  super.onStart();
  // Bind
  Intent intent = new Intent(getApplicationContext(), SampleService.class);
  bindService(intent, mySrviceConnection, Context.BIND_AUTO_CREATE);//サービスをバインド
 }

 @Override
 public void onStop() {
  Log.i(TAB, "onStop");
  super.onStop();
  // Unbind
  unbindService(mySrviceConnection);//サービスをアンバインド
 }

 private ServiceConnection mySrviceConnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO 自動生成されたメソッド・スタブ
   binder = (SampleService.LocalBinder) service;
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO 自動生成されたメソッド・スタブ

  }
 };

}
package com.example.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;

public class SampleService extends Service {

 private static final String TAB = "SampleService";
 private IBinder mBinder = new LocalBinder();

 public class LocalBinder extends Binder {
  void getService(TextView tv) {
   Log.i(TAB, "LocalBinder");//
   tv.setText("サービスから変更");
  }
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  Log.i(TAB, "onBind");//起動時に呼ばれる

  return mBinder;
 }

 @Override
 public void onDestroy() {
  Log.i(TAB, "onDestroy");
 }
}


サービスでスレッドを利用して定期的にUIを更新
package com.example.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.bindservice.SampleService.LocalBinder;

public class MainActivity extends Activity {

 private static final String TAB = "MainActivity";
 private TextView tv;
 private LocalBinder binder;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.textView1);

  Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Log.i(TAB, "ボタン2が押された");
    if (binder != null){
     Log.i(TAB, "binder は null ではない");
     binder.getService(tv);
    }else{
     Log.i(TAB, "binder は null");
    }
   }
  });
 }

 @Override
 public void onStart() {
  Log.i(TAB, "onStart");
  super.onStart();
  // Bind
  Intent intent = new Intent(getApplicationContext(), SampleService.class);
  bindService(intent, mySrviceConnection, Context.BIND_AUTO_CREATE);//サービスをバインド
 }

 @Override
 public void onStop() {
  Log.i(TAB, "onStop");
  super.onStop();
  // Unbind
  unbindService(mySrviceConnection);//サービスをアンバインド
 }

 private ServiceConnection mySrviceConnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO 自動生成されたメソッド・スタブ
   binder = (SampleService.LocalBinder) service;
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO 自動生成されたメソッド・スタブ

  }
 };

}
package com.example.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class SampleService extends Service {

 private Handler handler;

 public class MyThread implements Runnable {

  private int data;

  @Override
  public void run() {
   // TODO 自動生成されたメソッド・スタブ
   while (true) {
    Log.i(TAB, "run::" + data);//二番目に呼ばれる

    data++;
    Message msg = handler.obtainMessage();
    msg.arg1 = data;
    handler.sendMessage(msg);
    try {
     Thread.sleep(2000);
    } catch (InterruptedException e) {
     // TODO 自動生成された catch ブロック
     e.printStackTrace();
    }
   }
  }

 }

 private static final String TAB = "SampleService";
 private IBinder mBinder = new LocalBinder();

 public class LocalBinder extends Binder {

  void getService(final TextView tv) {
   Log.i(TAB, "LocalBinder");//
   //tv.setText("setData から変更");//ここでも変更可能

   new Thread(new MyThread()).start();

   /*
   //動くがメモリーリークの可能性がある
   //This Handler class should be static or leaks might occur
   handler = new Handler() {
    public void handleMessage(Message msg) {
     //updata.update(tv, data);
     Log.i(TAB, "handleMessage::" + msg);

     tv.setText(String.valueOf(msg.arg1));//ここでも変更可能

    }
   };
   */
   handler = new MyHandler(tv);
  }
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  Log.i(TAB, "onBind");//起動時に呼ばれる

  return mBinder;
 }

 @Override
 public void onDestroy() {
  Log.i(TAB, "onDestroy");
 }

 public static class MyHandler extends Handler {
  private TextView tv;

  public MyHandler(TextView tv) {
   // TODO 自動生成されたコンストラクター・スタブ
   this.tv = tv;
  }

  @Override
  public void handleMessage(Message msg) {
   Log.i(TAB, "handleMessage::" + msg);
   tv.setText(String.valueOf(msg.arg1));//ここでも変更可能
  }

 }
}

サービスで変更された値をアクティビティに戻してUIを変更する。
package com.example.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.bindservice.SampleService.LocalBinder;
import com.example.bindservice.SampleService.UpdateData;

public class MainActivity extends Activity {

 private static final String TAB = "MainActivity";
 private TextView tv;
 private LocalBinder binder;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.textView1);

  Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Log.i(TAB, "ボタン2が押された");
    if (binder != null) {
     Log.i(TAB, "binder は null ではない");
     binder.getService(new UpdateData() {

      @Override
      public void update(int data) {
       // TODO 自動生成されたメソッド・スタブ
       tv.setText(data + "");
      }
     });

    } else {
     Log.i(TAB, "binder は null");
    }
   }
  });
 }

 @Override
 public void onStart() {
  Log.i(TAB, "onStart");
  super.onStart();
  // Bind
  Intent intent = new Intent(getApplicationContext(), SampleService.class);
  bindService(intent, mySrviceConnection, Context.BIND_AUTO_CREATE);//サービスをバインド
 }

 @Override
 public void onStop() {
  Log.i(TAB, "onStop");
  super.onStop();
  // Unbind
  unbindService(mySrviceConnection);//サービスをアンバインド
 }

 private ServiceConnection mySrviceConnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO 自動生成されたメソッド・スタブ
   binder = (SampleService.LocalBinder) service;
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO 自動生成されたメソッド・スタブ

  }
 };

}
package com.example.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class SampleService extends Service {

 public interface UpdateData {
  public void update(int data);
 }

 private Handler handler;

 public class MyThread implements Runnable {

  private int data;

  @Override
  public void run() {
   // TODO 自動生成されたメソッド・スタブ
   while (true) {
    Log.i(TAB, "run::" + data);//二番目に呼ばれる

    data++;
    Message msg = handler.obtainMessage();
    msg.arg1 = data;
    handler.sendMessage(msg);
    try {
     Thread.sleep(2000);
    } catch (InterruptedException e) {
     // TODO 自動生成された catch ブロック
     e.printStackTrace();
    }
   }
  }

 }

 private static final String TAB = "SampleService";
 private IBinder mBinder = new LocalBinder();

 public class LocalBinder extends Binder {

  void getService(UpdateData updata) {
   Log.i(TAB, "LocalBinder");//
   //tv.setText("setData から変更");//ここでも変更可能

   new Thread(new MyThread()).start();

   handler = new MyHandler(updata);
  }
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  Log.i(TAB, "onBind");//起動時に呼ばれる

  return mBinder;
 }

 @Override
 public void onDestroy() {
  Log.i(TAB, "onDestroy");
 }

 public static class MyHandler extends Handler {
  private UpdateData updata;

  public MyHandler(UpdateData updata) {
   // TODO 自動生成されたコンストラクター・スタブ
   //this.tv = tv;
   this.updata = updata;
  }

  @Override
  public void handleMessage(Message msg) {
   Log.i(TAB, "handleMessage::" + msg);
   //tv.setText(String.valueOf(msg.arg1));//ここでも変更可能
   updata.update(msg.arg1);
  }

 }
}

時間を表示してみる。
package com.example.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.bindservice.SampleService.LocalBinder;
import com.example.bindservice.SampleService.UpdateData;

public class MainActivity extends Activity {

 private static final String TAB = "MainActivity";
 private TextView tv;
 private LocalBinder binder;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.textView1);

  Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Log.i(TAB, "ボタン2が押された");
    if (binder != null) {
     Log.i(TAB, "binder は null ではない");
     binder.getService(new UpdateData() {

      @Override
      public void update(String data) {
       // TODO 自動生成されたメソッド・スタブ
       tv.setText(data + "");
      }
     });

    } else {
     Log.i(TAB, "binder は null");
    }
   }
  });
 }

 @Override
 public void onStart() {
  Log.i(TAB, "onStart");
  super.onStart();
  // Bind
  Intent intent = new Intent(getApplicationContext(), SampleService.class);
  bindService(intent, mySrviceConnection, Context.BIND_AUTO_CREATE);//サービスをバインド
 }

 @Override
 public void onStop() {
  Log.i(TAB, "onStop");
  super.onStop();
  // Unbind
  unbindService(mySrviceConnection);//サービスをアンバインド
 }

 private ServiceConnection mySrviceConnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO 自動生成されたメソッド・スタブ
   binder = (SampleService.LocalBinder) service;
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO 自動生成されたメソッド・スタブ

  }
 };

}
package com.example.bindservice;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class SampleService extends Service {

 public interface UpdateData {
  public void update(String data);
 }

 private static final String TAB = "SampleService";
 private IBinder mBinder = new LocalBinder();

 public class LocalBinder extends Binder {

  void getService(UpdateData updata) {
   Log.i(TAB, "LocalBinder");//
   //tv.setText("setData から変更");//ここでも変更可能

   //new Thread(new MyThread()).start();

   MyHandler handler = new MyHandler(updata);
   handler.sendEmptyMessage(1);
  }
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  Log.i(TAB, "onBind");//起動時に呼ばれる

  return mBinder;
 }

 @Override
 public void onDestroy() {
  Log.i(TAB, "onDestroy");
 }

 public static class MyHandler extends Handler {
  private UpdateData updata;

  public MyHandler(UpdateData updata) {
   // TODO 自動生成されたコンストラクター・スタブ
   //this.tv = tv;
   this.updata = updata;
  }

  @Override
  public void handleMessage(Message msg) {
   Log.i(TAB, "handleMessage::" + msg);

   GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"));
   String data = gc.get(Calendar.HOUR_OF_DAY) + ":" + gc.get(Calendar.MINUTE) + ":" + gc.get(Calendar.SECOND);

   sendEmptyMessageDelayed(1, 1000);
   
   updata.update(data);
  }

 }
}




2011年12月24日土曜日

端末起動時にサービスを実行:タイマーで指定時間から繰り返し

端末起動時にサービスを実行:タイマーで繰り返し の記事の一部を修正

package com.my.android.test;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service {
 private Timer timer;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
   public void onCreate() {
     super.onCreate();
     Log.d("TAG", "onCreate");
   }
 @Override
 //onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
 //public void onStart(Intent intent, int startId) {
 public int onStartCommand(Intent intent, int flags, int startId) {
  //現在時刻の取得
  long currentTimeMillis = System.currentTimeMillis();
  Date date = new Date(currentTimeMillis + 10000);//現在時刻+10秒

  // タイマの設定
  timer = new Timer(true);
  final Handler handler = new Handler();
  //service = Executors.newSingleThreadScheduledExecutor();
  timer.scheduleAtFixedRate( new TimerTask() {
   @Override
   public void run() {
    handler.post(new Runnable() {
     public void run() {
       Log.d("TAG", "onStartCommand");
     }
    });
   }
  }, date, 3000);//スタート時間、3秒間隔
  //super.onStart(intent, startId);
  return START_STICKY;
 }
 @Override
   public void onDestroy() {
     super.onDestroy();
     if(timer != null){
       timer.cancel();
     }
     Log.d("TAG", "onDestroy");
   }

}

Executorsクラスで繰り返し

端末起動時にサービスを実行:タイマーで繰り返しの記事の一部を修正


package com.my.android.test;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service {
 private Timer timer;
 private ScheduledExecutorService service;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
   public void onCreate() {
     super.onCreate();
     Log.d("TAG", "onCreate");
   }
 @Override
 //onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
 //public void onStart(Intent intent, int startId) {
 public int onStartCommand(Intent intent, int flags, int startId) {
  
  final Handler handler = new Handler();
  service = Executors.newSingleThreadScheduledExecutor();
  service.scheduleAtFixedRate( new TimerTask() {
   @Override
   public void run() {
    handler.post(new Runnable() {
     public void run() {
       Log.d("TAG", "onStartCommand");
     }
    });
   }
  }, 1000, 3000, TimeUnit.MILLISECONDS);//遅延、3秒間隔、単位時間
  //super.onStart(intent, startId);
  return START_STICKY;
 }
 @Override
   public void onDestroy() {
     super.onDestroy();
     if(timer != null){
       timer.cancel();
     }
     Log.d("TAG", "onDestroy");
   }

}

2011年12月22日木曜日

端末起動時にサービスを実行:タイマーで繰り返し


package com.my.android.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MainReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  Log.v("タグ", "onReceive");
  // サービスを起動する(BOOT_COMPLETED時)
  context.startService(new Intent(context, MainService.class));
 }
}


package com.my.android.test;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service {
 private Timer timer;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
   public void onCreate() {
     super.onCreate();
     Log.d("TAG", "onCreate");
   }
 @Override
 //onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
 //public void onStart(Intent intent, int startId) {
 public int onStartCommand(Intent intent, int flags, int startId) {
  // タイマの設定
  timer = new Timer(true);
  final Handler handler = new Handler();
  timer.schedule(new TimerTask() {
   @Override
   public void run() {
    handler.post(new Runnable() {
     public void run() {
       Log.d("TAG", "onStartCommand");
     }
    });
   }
  }, 1000, 3000);//3秒間隔
  //super.onStart(intent, startId);
  return START_STICKY;
 }
 @Override
   public void onDestroy() {
     super.onDestroy();
     if(timer != null){
       timer.cancel();
     }
     Log.d("TAG", "onDestroy");
   }

}


package com.my.android.test;

import android.app.Activity;
import android.os.Bundle;

public class MyAndroidTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.android.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyAndroidTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name="MainReceiver" >
            <intent-filter >

                <!-- デバイスブート時のインテントを受け取るレシーバ -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name="MainService" >
        </service>
    </application>

</manifest>

Serviceのライフサイクル


サービスの開始

onCreate:サービスが作成された際に呼び出し

onStart:サービスが開始されたタイミングで呼び出される

サービスの実行

onDestroy:サービスが破棄された際に呼び出し

サービスの終了

2011年12月21日水曜日

AVDに端末起動のシグナルを送る

コマンドプロンプトを起動させる。

E:\soft\android-sdk-windows\platform-tools>adb shell
# am broadcast -a android.intent.action.BOOT_COMPLETED

2011年12月19日月曜日

端末起動時にサービスを起動させる

レシーバーで端末が起動したのを受け取って、
サービスを起動させる。

package com.android.my.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  return null;
 }

 public void onCreate() {
  Log.d("タグ", "onCreate");
 }

 public void onStart(Intent intent, int StartId) {
  super.onStart(intent, StartId);
  // stopSelf();
  Log.d("タグ", "onStart");
 }

 public void onDestroy() {
  Log.d("タグ", "onDestroy");
 }

}


package com.android.my.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
         Log.d("タグ", "onReceive");
            context.startService(new Intent(context, MyService.class));
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.my.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <service android:name="MyService" >
        </service>

        <receiver
            android:name="BootReciever"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <activity
            android:label="@string/app_name"
            android:name=".AdnroidServiceTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

BOOT_COMPLETEDをReceiveするアプリの場合、
実際にBOOT_COMPLETEDがBroadcastされるのはシステム起動時だけなので、
動作確認はAVDにコマンドプロンプトからamコマンドからインテントを投げて確認する。
E:\soft\android-sdk-windows\platform-tools>adb shell
# am broadcast -a android.intent.action.BOOT_COMPLETED 

アクティビティーはプロジェクト作成時に自動的に作成されたのを残しています。
必要ないのでマニフェストのアクティビティー関連の設定を削除して、
アプリの上書きをした場合は動きますが、
アプリを削除して、再インストールした場合は動きません。

Serviceを使ってみる


実行結果
12-19 06:51:44.123: D/タグ(5143): onCreate
12-19 06:51:44.123: D/タグ(5143): onStart
12-19 06:51:44.292: D/タグ(5143): onDestroy


package com.android.my.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class AdnroidServiceTestActivity extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // setContentView(R.layout.main);//アックティビティーに追加
  Intent intent = new Intent(this, MyService.class);
  startService(intent);
 }

}


package com.android.my.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

 @Override
 public IBinder onBind(Intent intent) {
  // TODO 自動生成されたメソッド・スタブ
  return null;
 }

 public void onCreate() {
  Log.d("タグ", "onCreate");
 }

 public void onStart(Intent intent, int StartId) {
  Log.d("タグ", "onStart");
  stopSelf();
 }

 public void onDestroy() {
  Log.d("タグ", "onDestroy");
 }

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.my.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="14" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AdnroidServiceTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="MyService" >
        </service>
    </application>
</manifest>