2010. 3. 30. 10:19

서비스 등록





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


  <receiver android:name=".service.MyStartupIntentReceiver">
            <intent-filter>
           
              <action android:name="android.intent.action.BOOT_COMPLETED" /> 
              <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>       
  <service android:name=".service.MyService">
     <intent-filter>
       <action android:name=".service.MyService" />
     </intent-filter>
    </service>    

package com.qnsolv.android.lifelog7.service;

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

public class MyStartupIntentReceiver extends BroadcastReceiver {

 public void onReceive(Context context, Intent intent) {
  if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
   Log.w("lifeLog", " MyStartupIntentReceiver  ");

   Intent serviceIntent = new Intent();
   serviceIntent.setAction(".service.MyService");
   context.startService(serviceIntent);
  }
 }
}

package com.qnsolv.android.lifelog7.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
 private Timer timer;
 private TimerTask executeAgain = new TimerTask() {
  @Override
  public void run() {
   Log.i("shservice", "!!!!!!!!!!!!!!");
   ContentResolver cr = getContentResolver();
   try{
    LocationManager locationManager;
    Log.i("shservice", "!!!!!!!!!!!!!!2");
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Log.i("shservice", "!!!!!!!!!!!!!!3");
    Location location = locationManager
      .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Log.i("shservice", "!!!!!!!!!!!!!!4");
    Log.i("shservice", "!!!!!!!!!!!!!!5" + location);
    String latLongString;

    if (location != null) {
     double lat = location.getLatitude();
     double lng = location.getLongitude();
     latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
     latLongString = "No location found";
    }

    Log.i("shservice", "!!!!!!!!!!!!!!6= "+latLongString);
    System.out.println("latLongString=="+latLongString);
    // Do something
   }catch(Exception e){
    e.printStackTrace();
    System.out.println();
    
   }

  }
 };

 @Override
 public void onStart(Intent intent, int startId) {
  if (timer == null) {
   timer = new Timer("Alert timer");
  }

  timer.scheduleAtFixedRate(executeAgain, 0, 10000);

 }

 @Override
 public void onDestroy() {
  this.stopSelf();
  Log.i("AlertService", "AlertService is being destroyed!!!");
 }

 @Override
 public void onCreate() {
  timer = new Timer("Alert timer");
 }

 @Override
 public IBinder onBind(Intent arg0) {
  return null;
 }
}