我正在尝试使用我的活动发送的意图来调用我的服务的某些功能。
这是我从活动发送意图的方式(在UI线程中):
Intent it = new Intent(MyService.INTENT_ACTIVITY_POSITION_CHANGED);
it.setClass(getApplicationContext(), MyService.class);
it.putExtra("posPercentX", x);
it.putExtra("posPercentY", y);
startService(it);
这就是我的 onStartCommand 在MyService中的样子:
@Override
public int onStartCommand (Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
if(intent.getAction().equals(INTENT_ACTIVITY_START)){
Toast.makeText(this,"onStartCommand ...", Toast.LENGTH_SHORT).show();
}else if(intent.getAction().equals(INTENT_ACTIVITY_POSITION_CHANGED)){
// here comes some code to get extras from intent
Log.d("INTENT_ACTIVITY_POSITION_CHANGED", "x=" + posX + " y=" + posY);
//TODO preveri
}
return startId;
}
我接到几个电话后:
ERROR/ActivityManager(52): java.lang.IllegalArgumentException: Unknown service start result: 4
为什么会出现此错误?任何想法?
答案 0 :(得分:5)
为什么你认为你应该回归startId
? onStartCommand()的文档是
intent 提供给startService(Intent)的Intent,如给定的那样。这个 如果服务正在,则可以为null 在其进程消失后重新启动 离开了,之前已经回来了 什么以外的 START_STICKY_COMPATIBILITY。
标志有关此启动请求的其他数据。目前要么0, START_FLAG_REDELIVERY,或 START_FLAG_RETRY。
startId 表示此特定请求的唯一整数 开始。与stopSelfResult(int)一起使用。 的返回强>
* The return value indicates what semantics the system should use for
服务的当前启动状态。 它可能是常数之一 与...相关联 START_CONTINUATION_MASK位
因此您需要返回START_STICKY或http://developer.android.com/reference/android/app/Service.html#START_CONTINUATION_MASK
中的一个值