androidn00b
androidn00b

Reputation: 31

Android Service Not Starting From Activity

I'm new here and I have a question on android services. For some reason my service is not starting. And not giving any errors in the logcat. I used

startService(new Intent(getApplicationContext(),MyResultsService.class));

to call the service (MyResultsService.class) in the main activity.

Also, This is MyResultsService.class:

package com.example.myapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyResultsService extends Service{
    public UpdateMyResults updater;
    public boolean stopThread = false;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d(getPackageName(), "Created MyResultsService");
    }

    @Override
    public synchronized int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.d(getPackageName(), "Starting...");
        if (!updater.isAlive()){
            updater = new UpdateMyResults();
            Log.d(getPackageName(), "New updater thread...");
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public synchronized void onDestroy() {
        // TODO Auto-generated method stub
        stopThread = true;
        Log.d(getPackageName(), "Destroying MyResultsService");
        try {
            updater.join();
            Log.d(getPackageName(), "Closed updater thread");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private class UpdateMyResults extends Thread{

        static final long DELAY = 30000;
        @Override
        public void run() {
            while (!stopThread){
                try {
                    //Do stuff and pause
                    Log.d(getName(), "Running");
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    // Interrupt
                    e.printStackTrace();
                }
            }//end while
        }//end run
    }//end UpdateMyResults
}

my xml manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />

    <application
        android:label="MyApp"
        android:icon="@drawable/ic_launcher"
        android:theme="@android:style/Theme.Holo.Light">

        <activity
            android:name=".MainActivity"
            android:label="My App">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>      
        </activity>

        <service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" />

    </application>

</manifest>

Any help would be greatly appreciated, Thanks!

Upvotes: 2

Views: 14264

Answers (5)

Aman Srivastava
Aman Srivastava

Reputation: 973

Line 23, you are calling a method on a variable (updater) that has not been initialized yet, causing a NPE. – Alex Fu Jul 15 '13 at 16:03

and you haven't started thread.

Upvotes: 0

Yuva Raj
Yuva Raj

Reputation: 3881

Try using the following in the Manifest file:

<service android:enabled="true" android:name="com.example.myapp.MyResultsService"/> 

Upvotes: 0

Brijesh Thakur
Brijesh Thakur

Reputation: 6788

Your manifest is having wrong Service Name. It's MYRESULTSSERVICE but your service class name is MyResultsService. Try changing from MYRESULTSSERVICE to MyResultsService

Upvotes: 0

Dulanga
Dulanga

Reputation: 1346

change your service tag in the manifest to the following.

<service android:enabled="true" android:name="com.example.myapp.MyResultsService" />

Upvotes: 0

Alex Fu
Alex Fu

Reputation: 5529

The name in <service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" /> must match EXACTLY as your class. Try not using all caps.

Upvotes: 14

Related Questions