我正在开发一个将“手势”与活动相关联的应用程序(例如 Dolphin Browser 与 url 相关联)。我想要做的是允许用户使用 Action.PICK_ACTIVITY 选择一个活动:
Intent data = new Intent(Intent.ACTION_MAIN);
data.addCategory(Intent.CATEGORY_LAUNCHER);
Intent intent = new Intent(Intent.ACTION_PICK_ACTIVITY);
intent.putExtra(Intent.EXTRA_INTENT, data);
startActivityForResult(intent, PICK_ACTIVITY);
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_ACTIVITY && resultCode == RESULT_OK) {
Gesture gesture = new Gesture();
gesture.intent = data;
Intent intent = new Intent(this, GestureEditorActivity.class);
intent.putExtra("com.example.gesture.Gesture",gesture.toByteArray());
startActivity(intent);
}
}
然后,当用户绘制相关手势时,我开始活动:
Intent intent = GestureList.getInstance(getApplicationContext()).getIntentForGesture(gesture);//intent corresponds to data in onActivityResult method
if(intent != null)
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
Toast.makeText(this, R.string.unrecognized_gesture, Toast.LENGTH_LONG).show();
但我得到了一个 ActivityNotFoundException :
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN dat=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;component=com.android.calculator2/.Calculator;end }
这是我的 getIntentForGesture 方法:
public Intent getIntentForGesture(Gesture gesture)
{
float bestDist = Float.MAX_VALUE;
Gesture bestGesture = null;
for(Gesture g:this)
{
float dist = gesture.distance(g);
if(dist < bestDist)
{
bestDist = dist;
bestGesture = g;
}
}
if(bestGesture != null)
return bestGesture.intent;
else
return null;
}
抱歉我的英语不好,提前感谢您的回答