2013-02-02 52 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

.services包中的WifiSearchService?如果不是,你应该删除.services。如果它找不到服务,它不会启动它,并且它没有表明它找不到它,这是令人讨厌的。 – Mgamerz

+1

是的,它位于一个名为Services的包中。 – thedjaney

+1

我在那里扔了一堆日志语句,看它是否真的到达启动它的代码。不知道你是否已经尝试过,因为你的代码没有显示它。你也把这个包声明为Services,但是这个manifest显示为.services - 是否应该大写? – Mgamerz

回答

16

我能够通过改变运行服务将软件包名称改为com.cdobiz.wifimapper.services 并更改软件包名称清单中的服务。

+4

结论:软件包应该以小写字母开头..谢谢! – thedjaney

+0

@thedjaney你应该给这个答案..谢谢 – Nabin

1

我解决了我的意图服务还未启动问题通过使用

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

开始我的服务

4

我有同样的问题。我通过复制类内容代码文本来解决它。然后我通过右键单击包 - >新建 - >服务 - >服务(Intent) ,然后我将相同的代码粘贴到类中并且它可以工作,从而删除了该类并重新创建了相同的类。

我希望这将是有益的人

6

我通过确保在service在App-清单注册解决了这个问题。

0

我有同样的问题。 解决它我删除了Intenservice类,无线 我创建了一个新的类,使用:右键单击>新建>服务>服务(IntentService)