需要帮助 IntentService 未启动

Need help IntentService not starting

在尝试找出我的 IntentService 的问题所在但未成功后,(阅读和谷歌搜索)我决定在 stakeoverflow 上提问。我不知道这段代码有什么问题。我实际上想在收到另一个 activity 的结果后启动服务。我确实从 activity 得到结果,然后从我想启动此服务的地方开始。

服务启动代码

Intent templateCreationIntent = new Intent(getApplicationContext(), TemplateCreationService.class);
    templateCreationIntent.putExtra(TemplateCreationService.PARAM_IN_MSG,userName);
    startService(templateCreationIntent);

服务代码

public class TemplateCreationService extends IntentService{

private static final String TAG = "TemplateCreationService";
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;


static final String TEST_RAW_DATA_PATH = Environment.getExternalStorageDirectory() +"/gaitDataRecording"+ "/rawTestingData" + "/acc/";
public static String userName;
public static String cycleLength;
public static final String PARAM_OUT_MSG = "omsg";
public static final String PARAM_IN_MSG = "imsg";

public TemplateCreationService() {
    super(TemplateCreationService.class.getName());
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.d(TAG,"Template Creation Service Started");
    userName = intent.getStringExtra(PARAM_IN_MSG);

    try {
        Boolean b = DataProcessingStepsV2.gaitDataLoading(TRAIN_RAW_DATA_PATH, userName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    double []gaitCycleLengths = DataProcessingStepsV2.getCycleLengths();

    cycleLength = gaitCycleLengths.toString();

    // processing done here….
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra(PARAM_OUT_MSG,cycleLength);
    sendBroadcast(broadcastIntent); 
    }    
  }

清单

<service   android:name=".gait_authentication_segmentation.TemplateCreationService"
    android:exported = "false"/>

主要Activity注册接收器

 IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
 filter.addCategory(Intent.CATEGORY_DEFAULT);
 receiver = new ResponseReceiver();
 registerReceiver(receiver, filter); 

广播接收器

public class ResponseReceiver extends BroadcastReceiver {
       public static final String ACTION_RESP =    
          "at.usmile.gait_authentication.intent.action.MESSAGE_PROCESSED";

       @Override
        public void onReceive(Context context, Intent intent) {
          Toast.makeText(getApplicationContext(), intent.getStringExtra(TemplateCreationService.PARAM_OUT_MSG),Toast.LENGTH_LONG).show();
           //String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
           //result.setText(text);
        }
    }

我认为 AndroidManifest.xml 中的服务名称不正确。它可以是:

  1. 这不是完全限定的名称。
  2. 有些拼写错误。

尝试使用服务名称提供完整的包名称并试一试。