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>

関連記事

0 件のコメント:

コメントを投稿