2013-02-02 19 views
7

Wiem, że to pytanie było zadawane wiele razy, ale wszystkie inne wątki nie rozwiązały w ogóle mojego problemu, nie widzę niczego złego w moim kodzie. Może coś tu przeoczyłem, czy ktoś może mi pomóc?Android IntentService nie uruchamia się

Kod zamiarem 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) { 
       } 
      } 
     } 
    } 

} 

Uruchomienie usługi przestawiając przełącznik:

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; 
    } 

} 

Oczywisty:

<?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> 
+3

Czy WifiSearchService w pakiecie .services? Jeśli nie, powinieneś usunąć .services. Jeśli nie może znaleźć usługi, nie uruchomi jej i nie da żadnego wskazania, że ​​nie może jej znaleźć, co jest dość irytujące. – Mgamerz

+1

Tak, jest w pakiecie o nazwie Usługi. – thedjaney

+1

Wrzuciłbym tam kilka instrukcji do dziennika, aby sprawdzić, czy rzeczywiście dociera do kodu, który go uruchamia. Nie wiem, czy próbowałeś tego jeszcze, ponieważ Twój kod go nie wyświetla. Również deklarujesz pakiet jako Usługi, ale manifest pokazuje go jako .services - czy powinien być pisany wielkimi literami? – Mgamerz

Odpowiedz

16

udało mi się uruchomić usługę poprzez zmianę nazwę pakietu na com.cdobiz.wifimapper.services i zmień nazwę pakietu usługi w manifeście.

+4

Wniosek: pakiety powinny rozpoczynać się od małych liter. Dzięki! – thedjaney

+0

@thedjaney powinieneś dać odpowiedź na to .. dzięki – Nabin

1

I rozwiązać problem z moim zamiarem Usługa nie wychodząc za pomocą

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

zacząć moje życzenie

4

miałem ten sam problem. Rozwiązałem go, kopiując tekst kodu treści klasy. Następnie usunąłem klasę i utworzyłem tę samą klasę ponownie, klikając prawym przyciskiem myszy pakiet -> Nowa -> Usługa -> Usługa (zamiar) Następnie wkleiłem ten sam kod do klasy i to działa.

Mam nadzieję, że będzie pomocny dla kogoś

6

I rozwiązać problem, upewniając się, że service jest zarejestrowana w App-manifestu.

0

Miałem ten sam problem. Aby rozwiązać ten problem, usunąłem klasę Intenservice, w porównaniu do utworzyłem nową klasę za pomocą: prawy przycisk myszy> nowy> Usługa> Usługa (IntentService)

Powiązane problemy