This is  definitely a viable approach for my application architecture, but
still strugglng to make this work. Here are some details, and I am hoping
that someone will point out what is going wrong here.

1. I have a class "MyServiceInitiator" that is hooked up to the
BOOT_COMPLETED action.


public class MyServiceInitiator extends BroadcastReceiver {

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder
service) {
        }

        public void onServiceDisconnected(ComponentName className) {
         }
    };

    public MyServiceInitiator() {
        System.out.println ("MyServiceInitiator
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println ("onReceive
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        context.startService(new Intent("StartMyService"));
        System.out.println
("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    }

}

2. I created my service class next:

public class MyService extends Service implements Runnable {


    @Override
    public void onCreate() {
        System.out.println ("MyService:
onCreate()================================================");
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        System.out.println ("MyService:
onBind()================================================");
        doScan();
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        System.out.println ("MyService  ))))))))))))))))))))))))))");
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println
("onDestroy()================================================");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println
("onUnbind()================================================");
        return super.onUnbind(intent);
    }

    public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }


    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public void run() {
        try {
            System.out.println ("MyService(): inside the run method....");
            Thread.currentThread().sleep(1000);
        }
        catch(InterruptedException iex) {
            iex.printStackTrace();
        }

    }


}

3. Here is the android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.mypackage"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:enabled="true" android:icon="@drawable/icon"
android:label="@string/app_name">


    <receiver android:name=".MyServiceInitiator" android:enabled="true"
android:exported="true">
            <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"
/>
            </intent-filter>
    </receiver>

        <service android:name=".MyService" >
            <intent-filter>
               <action android:name="StartMyService" />
            </intent-filter>
         </service>

    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

I need to use a thread to do some background process that does not seem to
run at all. The android documentation stays clearly that we should spawn our
own threads if we need to perform long standing operations.

I also see this log cat out that clearly shows that the process
corresponding to the service crashed:

D/ActivityManager(  584): Force removing process ProcessRecord{43701018 703:
com.bn/10020} (com.bn/10020)
W/ActivityManager(  584): Scheduling restart of crashed service
com.mypackage/.MyService in 5000ms
I/Process (  584): Sending signal. PID: 703 SIG: 9

Any idea, if what I am trying to achieve is even possible? What am i doing
wrong?

Thanks

Ravi



On Thu, Jul 23, 2009 at 11:48 AM, Mark Murphy <mmur...@commonsware.com>wrote:

>
> > I have a need to create a background service that starts up during the
> > system boot up, and keeps running until the device is powered down. There
> > is
> > no UI or Activity associated with this. I created a class extending the
> > android Service class, and added the setttings to the AndroidManifest.xml
> > file. When I launch this service in the emulator, I don't see this
> > launched
> > at all. I have overrided almost all the methods in the Service class to
> > put
> > log statements, but none of them shows up.
>
> Use a BroadcastReceiver (<receiver>) for BOOT_COMPLETED, and have that
> BootReceiver start the service. AFAIK, Android will not directly start up
> a service for that Intent.
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com
> Android App Developer Books: http://commonsware.com/books.html
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to