我正在尝试从片段选项卡启动 IntentService,但我没有回应。我的片段中的代码如下:
private Intent prepareIntent(boolean isSending) {
Intent localIntent = new Intent(getActivity(), StartIActivity.class);
Log.d(THIS_FILE, "StartIActivity");
localIntent.putExtra("incoming", isSending);
localIntent.putExtra("remote_contact", setValidNumber(callUri));
localIntent.putExtra("acc_id", this.accId);
return localIntent;
}
private void startIAService(boolean bool) {
Log.d(THIS_FILE, "Start Service");
Context ctx = (Context) myFragment.this.getActivity();
ctx.startService(prepareIntent(bool));
return;
}
我的意图服务类是:
public class StartIActivity extends IntentService {
public StartIActivity() {
super("StartIActivity");
}
protected void onHandleIntent(Intent it) {
Intent intent = new Intent(it);
intent.setClass(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d("IActivity", "Start Activity");
}
}
运行 startIAservice 时,prepereIntent 已运行,但无法启动该服务。我需要使用 IntentService 因为我想一次执行一项任务,但我不明白如何。这里有什么帮助,最好的代码实现是什么?