Android IntentService无法启动

时间:2013-02-02 04:16:03

标签: 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>

6 个答案:

答案 0 :(得分:16)

我可以通过将包名更改为com.cdobiz.wifimapper.services来运行您的服务 并更改清单中服务的包名称。

答案 1 :(得分:7)

我通过确保在App-Manifest中注册了service来解决了这个问题。

答案 2 :(得分:5)

我遇到了同样的问题。我通过复制类内容代码文本解决了这个问题。然后我通过右键单击包删除了该类并再次创建了同一个类 - &gt;新 - &gt;服务 - &gt;服务(意向) 然后我将相同的代码粘贴到类中并且它可以工作。

我希望这会对某人有所帮助

答案 3 :(得分:2)

我解决了我的意图服务问题,而不是使用

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

启动我的服务

答案 4 :(得分:1)

如果清单中有android:enabled="true",请在清单中尝试

答案 5 :(得分:0)

我遇到了同样的问题。 为了解决它,我删除了Intenservice类,而不是 我创建了一个新类:右键单击&gt; new&gt; Service&gt; Service(IntentService)