在嘗試找到我的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);
}
}
它是否在ontentandvice的onHandleIntent? – 2015-02-24 06:09:58
我試圖調試但它不會去onHandleIntent(Intent intent)方法 – Muhammad 2015-02-24 06:22:23
檢查manifest.xml中的服務聲明,即名稱是否正確,是否是完全限定名稱。 – 2015-02-24 06:30:03