2013-02-02 15 views
7

So che questa domanda è stata posta molte volte, ma tutti gli altri thread non hanno risolto il mio problema, non riesco a vedere nulla di sbagliato nel mio codice. Forse mi sono perso qualcosa qui, qualcuno può aiutarmi?Android IntentService non avviato

codice per il servizio Intent:

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

} 

Avvio del servizio per mezzo di un pulsante:

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

} 

manifesto:

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

WifiSearchService è nel pacchetto .services? Se non lo è, dovresti rimuovere i servizi. Se non riesce a trovare il servizio, non lo avvia, e non dà indicazioni che non lo possa trovare, il che è piuttosto fastidioso. – Mgamerz

+1

Sì, si trova all'interno di un pacchetto chiamato Servizi. – thedjaney

+1

Lanciavo un mucchio di istruzioni di registro lì per vedere se effettivamente sta raggiungendo il codice che lo lancia. Non sono sicuro di averlo provato dal momento che il tuo codice non lo mostra. Inoltre dichiari il pacchetto come Servizi, ma il manifest lo mostra come .services - dovrebbe essere in maiuscolo? – Mgamerz

risposta

16

ero in grado di eseguire il servizio cambiando il nome del pacchetto in com.cdobiz.wifimapper.services e cambia il nome del pacchetto del servizio nel manifest.

+4

Conclusione: i pacchetti dovrebbero iniziare con caratteri minuscoli .. Grazie! – thedjaney

+0

@thedjaney dovresti dare una risposta a questo .. grazie – Nabin

1

ho risolto un problema con il mio servizio non Intento di partenza utilizzando

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

per iniziare il mio servizio

4

Ho avuto lo stesso problema. L'ho risolto copiando il testo del codice del contenuto della classe. Quindi ho cancellato la classe e creato di nuovo la stessa classe facendo clic con il tasto destro sul pacchetto -> Nuovo -> Servizio -> Servizio (Intento) Quindi ho incollato lo stesso codice alla classe e funziona.

Spero che questo sarà utile a qualcuno

6

ho risolto il problema facendo in modo che il service è registrato in App-manifesto.

0

Ho avuto lo stesso problema. Per risolvere il problema ho cancellato la classe Intenservice, con uno ho creato una nuova classe usando: tasto destro> nuovo> Servizio> Servizio (IntentService)

Problemi correlati