Search code examples
javaandroidandroid-intentbackground-service

Starting an Android Service


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.


Solution

  • 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.
        }
    }