【问题标题】:Android IntentService not startingAndroid IntentService 未启动
【发布时间】:2013-02-02 04:16:02
【问题描述】:

我知道这个问题已经被问过很多次了,但是所有其他线程都没有解决我的问题,我看不出我的代码有什么问题。也许我在这里遗漏了一些东西,有人可以帮我吗?

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

}

点击开关启动服务:

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>

【问题讨论】:

  • .services 包中有 WifiSearchService 吗?如果不是,您应该删除 .services。如果找不到服务,它就不会启动它,并且它没有给出找不到它的指示,这有点烦人。
  • 是的,它在一个名为 Services 的包中。
  • 我会在那里抛出一堆日志语句,看看它是否真的到达了启动它的代码。不确定您是否已经尝试过,因为您的代码没有显示它。此外,您将包声明为服务,但清单将其显示为 .services - 是否应该大写?
  • 我实际上尝试了 Log throwing,当它到达启动服务的代码时,它似乎没有做任何事情。而且也没有错误。此外,如果我将包名称大写,eclipse 似乎不接受它
  • Services 可能是一个 android 包。您应该尝试使用完整的包名。 com.yourcomp.name.services。

标签: android intentservice


【解决方案1】:

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

【讨论】:

  • 结论:包应该以小写开头..谢谢!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。我通过复制课程内容代码文本解决了它。然后我删除了该类并通过右键单击包 -> 新建 -> 服务 -> 服务(意图)再次创建了相同的类 然后我将相同的代码粘贴到类中,它就可以工作了。

    我希望这对某人有帮助

    【讨论】:

      【解决方案4】:

      我解决了我的 Intent 服务没有通过使用启动的问题

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

      启动我的服务

      【讨论】:

        【解决方案5】:

        如果是 ser,请尝试 android:enabled="true"in manifest

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题。 为了解决这个问题,我删除了 Intenservice 类,wi an than 我使用以下方法创建了一个新类:右键单击>new>Service>Service(IntentService)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多