2013-02-02 5 views
7

この質問は何度も尋ねられていますが、他のスレッドはすべて私の問題を解決していませんでした。多分私はここで何かを逃した、誰かが私を助けることができますか?テントサービスのAndroid IntentServiceが起動していません

コード:

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

} 

スイッチをフリックすることで、サービスの開始:

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

} 

マニフェスト:私は変更してサービスを実行することができました

<?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は.servicesパッケージにありますか?そうでない場合は、.servicesを削除する必要があります。それがサービスを見つけることができない場合、それはそれを開始しません、そして、それは迷惑なのですそれを見つけることができないという指示を与えません。 – Mgamerz

+1

はい、サービスと呼ばれるパッケージ内にあります。 – thedjaney

+1

私は実際にそれを起動するコードに到達しているかどうかを確認するためにそこにログステートメントの束を投げたいと思います。あなたのコードはそれを表示していないので、あなたがまだそれを試したかどうかは分かりません。また、パッケージをサービスとして宣言しますが、マニフェストには.servicesとして表示されます - 大文字にする必要がありますか? – Mgamerz

答えて

16

パッケージ名をcom.cdobiz.wifimapper.services に変更し、パッケージ名を変更しますマニフェストのサービスの

+4

結論:パッケージは小文字で始まるはずです。ありがとう! – thedjaney

+0

@thedjaneyあなたはこれに答えるべきです..ありがとう – Nabin

1

私は同じ問題を抱えていた私のサービス

4

を開始する

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

を使用して起動していない私の意図サービスに問題を解決しました。私はクラスの内容コードのテキストをコピーして解決しました。次に、クラスを削除して、パッケージ - >新規 - >サービス - >サービス(インテント)を右クリックして同じクラスを再度作成しました その後、同じコードをクラスに貼り付けて動作させます。私はこれが誰か

6

に参考になっことを願っ

私はserviceがアプリケーション・マニフェストに登録されていることを確認することで問題を解決しました。

0

私は同じ問題がありました。 これを解決するには、Intenserviceクラスを削除しました。 新しいクラスを作成しました。右クリック>新規>サービス>サービス(IntentService)

関連する問題