需要帮助IntentService无法启动

时间:2015-02-24 06:04:14

标签: android-intentservice

在尝试找到我的IntentService的问题失败后,(阅读和谷歌搜索)我决定在有关溢出流程时询问。我无法弄清楚这段代码有什么问题。我实际上想在收到另一个活动的结果后启动服务。我从该活动获得结果然后来到我想要启动此服务的方法。

服务入门代码

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"/>

注册接收者的主要活动

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

1 个答案:

答案 0 :(得分:0)

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

  1. 这不是完全限定名称。
  2. 有一些拼写错误。
  3. 尝试使用服务名称提供完整的包名称并尝试一下。