这是我的onReceive功能:
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<DetectedActivity> updatedActivities =
intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
int type = 0x0;
Integer confidence = 0;
Collections.sort(updatedActivities, new Comparator<DetectedActivity>() {
@Override
public int compare(DetectedActivity lhs, DetectedActivity rhs) {
if(lhs.getConfidence() > rhs.getConfidence()){
return -1;
}else if(lhs.getConfidence() > rhs.getConfidence()){
return 1;
}
return 0;
}
});
String rawData = "(";
for (DetectedActivity detectedActivity : updatedActivities){
if(detectedActivity.getConfidence() >= 25){
if(type==PSMotionService.unknown || type==PSMotionService.stationary) {
rawData += detectedActivity.getType() + ":" + detectedActivity.getConfidence() + ",";
Log.i("", "autopilot type detected activity: " + detectedActivity);
if (confidence < detectedActivity.getConfidence()) {
confidence = detectedActivity.getConfidence();
}
if (detectedActivity.getType() == DetectedActivity.STILL) {
type |= PSMotionService.stationary;
} else if (detectedActivity.getType() == DetectedActivity.IN_VEHICLE) {
type |= PSMotionService.automotive;
} else if (detectedActivity.getType() == DetectedActivity.ON_BICYCLE) {
type |= PSMotionService.bicycling;
} else if (detectedActivity.getType() == DetectedActivity.ON_FOOT || detectedActivity.getType() == DetectedActivity.WALKING) {
type |= PSMotionService.walking;
} else if (detectedActivity.getType() == DetectedActivity.RUNNING) {
type |= PSMotionService.running;
} else if (detectedActivity.getType() == DetectedActivity.UNKNOWN) {
type |= PSMotionService.unknown;
}
}
}
}
rawData += ")";
Long timestamp = System.currentTimeMillis()/1000;
Integer confidenceFlag = 0;
if(confidence >= 25 && confidence < 75){
confidenceFlag = 1;
}else if(confidence >=75){
confidenceFlag = 2;
}
Log.i("", "autopilot type is:" + type + "... timestamp: " + timestamp + "....confidence" + confidence + "...confidenceFlag:" + confidenceFlag);
Log.i("", "autopilot-----------------------------------------------END");
Motion activity = new Motion(timestamp, type, confidenceFlag);
Utils.appendLog("NEW MOTION: [" + activity.getTimestamp() + ", " + activity.getType() + ", " + activity.getConfidence() + "]" + rawData, "D", Constants.TRACKER);
if(PSTrip.getActiveTrip() != null){
PSMotionService.getInstance(context).motionsTrip.add(activity);
Log.i("", "autopilot added to trip: size is:" + PSMotionService.getInstance(context).motionsTrip.size());
if(PSApplicationClass.getInstance().pref.getGeoEnabled(context)) {
appendMotionActivity(context, type, activity);
}
}else{
Log.i("", "autopilot test to add to buffer");
appendMotionActivity(context, type, activity);
}
Log.i("", "autopilot-----------------------------------------------END AAAAALLLLLLL");
}
我会从意图中提取DetectedActivity List:
ArrayList<DetectedActivity> updatedActivities =
intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
但现在我遇到了这个问题:
/Users/alinrosu/Workspace/passenger-android/passenger/src/main/java/nl/hgrams/passenger/listeners/ActivityDetectionBroadcastReceiver.java
Error:(31, 51) error: incompatible types: inferred type does not conform to upper bound(s)
inferred: DetectedActivity
upper bound(s): Parcelable
Error:Execution failed for task ':passenger:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
如何在不解决此问题的情况下提取DetectedActivity列表?
答案 0 :(得分:1)
问题是您正在使用Java 8的语言级别设置,这会加剧对Java 7及更低版本中的自动转换的一些限制。有关更多详细信息,请参阅Why does this Java 8 program not compile?。
我没有测试这个的东西,所以我不能确定这是否有效,但你可以先尝试施展它:
ArrayList<DetectedActivity> updatedActivities =
(ArrayList<DetectedActivity>)intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
如果这不起作用,请这样做:
ArrayList<Parcelable> updatedActivities = intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
现在,当你使用数组时,你需要转换每个元素,如下所示:
for (Parcelable p : updatedActivities) {
// cast to correct type
DetectedActivity detectedActivity = (DetectedActivity)p;
// do whatever with "detectedActivity"...
}