1

I am creating one application related to BroadcastReceiver and Service.

My intention is that my service is started from boot.

For that I am using broadcast receivers and start my service through that.

It will work fine my service class is looking like as fallows.

public class Myservice extends Service 
{
    public void onCreate() 
    {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);

        mScrollback = new Vector<String>();
        Thread thr = new Thread(worker);
        thr.start();        
    }

    Runnable worker = new Runnable() 
    {
         public void run() 
         {
             runLog();
             return;
         }
    };
}

This is my service class, it is working fine.

Before my application will start, i am getting the information about service through this application.

When my application is run the service is not running.

If any one has the solution please let me know.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
kiran
  • 3,244
  • 7
  • 34
  • 57
  • Check out the following post: http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – ramdroid Feb 07 '12 at 12:48

2 Answers2

1

That's not how you do multithreading in Services. Look At the step "Extending the Service Class": http://developer.android.com/guide/topics/fundamentals/services.html

Multithreading in a Service Class is done with an Handler thread. Here is the code from the docs (from the link above)

public class HelloService extends Service {
  private Looper mServiceLooper;
  private ServiceHandler mServiceHandler;

  // Handler that receives messages from the thread
  private final class ServiceHandler extends Handler {
      public ServiceHandler(Looper looper) {
          super(looper);
      }
      @Override
      public void handleMessage(Message msg) {
          // 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) {
                  }
              }
          }
          // Stop the service using the startId, so that we don't stop
          // the service in the middle of handling another job
          stopSelf(msg.arg1);
      }
  }

  @Override
  public void onCreate() {
    // Start up the thread running the service.  Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block.  We also make it
    // background priority so CPU-intensive work will not disrupt our UI.
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler 
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
      // We don't provide binding, so return null
      return null;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
  }
}

Furthermore you have to make sure that your Broadcast Receiver is listed in the manifest and receives the BOOT_COMPLETED Intent:

<receiver android:name="MyBroadCast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
ezcoding
  • 2,974
  • 3
  • 23
  • 32
0

You need to give permission to start your service after your device gets rebooted. You can give permission as follows:

<service android:enabled="true" android:name=".Myservice"  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>
Dhruvisha
  • 2,538
  • 1
  • 21
  • 31