12

I know this question has been asked many times, but all the other threads didn't solve my issue at all, I can't see anything wrong with my code. Maybe I missed something here, can anyone help me out?

Code for the Intent Service:

package Services;

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

public class WifiSearchService extends IntentService {

  /** 
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */
  public WifiSearchService() {
      super("WifiSearchService");
  }

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

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // 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) {
              }
          }
      }
  }

}

Starting the service by flicking a switch:

package com.cdobiz.wifimapper;

import Services.WifiSearchService;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

public class MainActivity extends Activity {

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;

        setContentView(R.layout.activity_main);


        Switch serviceSwitch = (Switch) this.findViewById(R.id.switchService);

        serviceSwitch.setChecked(isMyServiceRunning());

        serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton view, boolean state) {

                if(state){
                    startService(new Intent(context, WifiSearchService.class));
                }else{
                    stopService(new Intent(context, WifiSearchService.class));
                }
            }

        });

    }

    private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            Log.v("debug", service.service.getClassName());
            if ("com.cdobiz.wifimapper.services.WifiSearchService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cdobiz.wifimapper"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cdobiz.wifimapper.MainActivity"
            android:label="@string/app_name" >
            <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=".services.WifiSearchService"></service>
    </application>

</manifest>
thedjaney
  • 1,126
  • 2
  • 10
  • 28
  • 3
    Is WifiSearchService in the .services package? If it's not you should remove the .services. If it can't find the service it won't start it, and it gives no indication that it can't find it, which is kind of annoying. – Mgamerz Feb 02 '13 at 04:33
  • 1
    Yes, it is inside a package called Services. – thedjaney Feb 02 '13 at 04:37
  • 1
    I'd throw a bunch of log statements in there to see if it's actually reaching the code that launches it. Not sure if you've tried that yet since your code doesn't show it. Also you declare the package as Services, but the manifest shows it as .services - should it be capitalized? – Mgamerz Feb 02 '13 at 04:45
  • I actually tried the Log throwing and it doesn't seem to do anything when it reaches the code that starts the service. And no errors too. Also, eclipse doesn't seem to accept it if I capitalize the Package name – thedjaney Feb 02 '13 at 04:53
  • Services might be an android package. You should try the full package name instead. com.yourcomp.name.services. – Mgamerz Feb 02 '13 at 04:57

6 Answers6

20

I was able to run your service by changing the package name to com.cdobiz.wifimapper.services and change the package name of the service in the manifest.

Samoka
  • 1,283
  • 2
  • 12
  • 23
9

I solved the problem by making sure that the service is registered in the App-Manifest.

Philipp Hofmann
  • 3,388
  • 26
  • 32
6

I had the same problem. I solved it by copying the class content code text. Then I deleted the class and created the same class again by right clicking the package -> New -> Service -> Service(Intent) Then I pasted the same code to the class and it works.

I Hope this will be helpfull to someone

Nabuska
  • 443
  • 9
  • 17
5

I solved a Problem with my Intent Service not starting by using

Intent intent = new Intent(this,UploadService.class);
    this.startService(intent);

to start my Service

2

try with android:enabled="true"in manifest if the ser

0

I had the same problem. To solve it I deleted the Intenservice class, wi an than i created a new class using: right click>new>Service>Service(IntentService)