0

在尝试找出我的 IntentService 的问题后,(阅读和谷歌搜索)我决定在 stakeoverflow 上提问。我无法弄清楚这段代码有什么问题。我实际上想在收到另一个活动的结果后启动一项服务。我确实从该活动中获得了结果,然后转到了我想要启动此服务的方法。

服务启动代码

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

1 回答 1

0

我认为 AndroidManifest.xml 中的 Service 名称不正确。有可能:

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

尝试使用服务名称给出完整的包名称并尝试一下。

于 2015-02-24T06:32:43.057 回答