意图中的 Bundle 中的映射在服务中的 onStartCommand 中为空
Map inside Bundle in intent is null in onStartCommand in Service
我正在尝试通过以下代码传递一些数据来启动服务:
Intent countDownTimerIntent = new Intent(getActivity(), CountDownTimerService.class);
Bundle bundle = new Bundle();
bundle.putLong(CountDownTimerService.DURATION, ((Test) mcqDataSet).getDuration());
countDownTimerIntent.putExtras(bundle);
getContext().startService(countDownTimerIntent );
现在,当我尝试从 Intent 接收包数据时,它显示包中有一些数据,但它的映射为空
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
final long duration = bundle.getLong(DURATION, 0l);
这是调试包的样子
您好,请使用以下代码发送带有数据的意图。
Intent countDownTimerIntent = new Intent(getActivity(), CountDownTimerService.class);
countDownTimerIntent.putExtra(CountDownTimerService.DURATION, ((Test) mcqDataSet).getDuration());
getContext().startService(countDownTimerIntent );
经过一番折腾,发现onStartCommand中的return类型应该是START_REDELIVER_INTENT。这是因为很多时候意图会丢失和无法实现。所以这个 return 类型强制它在需要获取数据时重新传递它。代码如下:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
if (bundle != null)
final long duration = bundle.getLong(DURATION, 0l);
return START_REDELIVER_INTENT;
}
我正在尝试通过以下代码传递一些数据来启动服务:
Intent countDownTimerIntent = new Intent(getActivity(), CountDownTimerService.class);
Bundle bundle = new Bundle();
bundle.putLong(CountDownTimerService.DURATION, ((Test) mcqDataSet).getDuration());
countDownTimerIntent.putExtras(bundle);
getContext().startService(countDownTimerIntent );
现在,当我尝试从 Intent 接收包数据时,它显示包中有一些数据,但它的映射为空
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
final long duration = bundle.getLong(DURATION, 0l);
这是调试包的样子
您好,请使用以下代码发送带有数据的意图。
Intent countDownTimerIntent = new Intent(getActivity(), CountDownTimerService.class);
countDownTimerIntent.putExtra(CountDownTimerService.DURATION, ((Test) mcqDataSet).getDuration());
getContext().startService(countDownTimerIntent );
经过一番折腾,发现onStartCommand中的return类型应该是START_REDELIVER_INTENT。这是因为很多时候意图会丢失和无法实现。所以这个 return 类型强制它在需要获取数据时重新传递它。代码如下:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
if (bundle != null)
final long duration = bundle.getLong(DURATION, 0l);
return START_REDELIVER_INTENT;
}