-5

Sorry if this is a dumb question but how do initiate an android service? I cant figure out the error code on public class scilentservice extends IntentService { (see code below). I think it might have something to do with the intent but i'm not positive. What I want is for my app to start the service which would then auto science the phone at certain times. Here is my code

/**
 * Created by Abe on 3/29/2015.
 */

import android.app.IntentService;
import android.content.Intent;

public class scilentservice extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}
`

Thanks in advance for your help.

Abe Zukor
  • 19
  • 3

2 Answers2

3

This question will get downvoted and possibly closed because it's basic information found with a simple Google Search. Please look at the documentation linked in the comments and then the next step on how to send a request… this is the Android documentation.

Snippet:

Create a new, explicit Intent for the IntentService called RSSPullService.

/*
 * Creates a new Intent to start the RSSPullService
 * IntentService. Passes a URI in the
 * Intent's "data" field.
 */
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));

Call startService()

// Starts the IntentService
getActivity().startService(mServiceIntent);

The intentService code:

public class RSSPullService extends IntentService {

    public RSSPullService() {
        super("RSSPullService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //Do the work here.
    }
}
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
0

I have used extends Intent instead and override the method onStartCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    return 1;
}

And you either start the service on some command sent from an activity or some notification sent to the application.

Aurel
  • 27
  • 8
  • That's not the same thing. An IntentService shouldn't override onStartCommand because that gets called by the base class. – Martin Marconcini Mar 29 '15 at 17:11
  • @MartínMarconcini Thats why I said I used a Service instead and it has worked – Aurel Mar 29 '15 at 17:14
  • Which is worse, because a `Service` is not the same thing as an `IntentService`, yes. If you use a service you have to override onStartCommand. But the user is not asking to use a Service as far as I can understand from his question. – Martin Marconcini Mar 29 '15 at 17:16
  • @MartínMarconcini The question says how to initiate an android service and thought this might help. – Aurel Mar 29 '15 at 17:21
  • You're right. I think this is a case of "The User Has No Clue". In any case, no further information was provided so I doubt there's much else to do here. – Martin Marconcini Mar 29 '15 at 17:22