我目前正在尝试实现一个自定义键盘,可以将图像(可能通过意图?)发送到某个应用程序。更具体地说,我试图在自定义键盘上创建一个键,将键盘发送到默认的消息传递应用程序,以便它可以作为彩信发送。
我已修改了示例SoftKeyboard项目以执行此操作。以下是我到目前为止的情况:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
mComposing.append((char) primaryCode);
// Send message here
Intent pictureMessageIntent = new Intent(Intent.ACTION_SEND);
pictureMessageIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/drawable/icon_en_gb");
pictureMessageIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(pictureMessageIntent);
getCurrentInputConnection().setComposingText(mComposing, 1);
updateShiftKeyState(getCurrentInputEditorInfo());
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}
}
问题是我得到一个运行时异常,说:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我不熟悉Android自定义键盘,但我不确定是否开始新的Activity是最好的主意。有没有人有任何建议?
答案 0 :(得分:3)
问题在于活动通常意味着从其他活动开始。 Android添加了此错误,以确保开发人员只是自愿而有意识地更改此流程。
在发送修复问题的意图之前添加以下行:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
答案 1 :(得分:1)
你需要完全按照它所说的做 - 在意图中添加FLAG_ACTIVITY_NEW_TASK标志。服务的任何意图都需要这样做。修复它,它应该工作(或至少开始活动)。
答案 2 :(得分:0)
把这一行:
pictureMessageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
当您创建此键盘以从uri
发送图像时,也请添加此行:
// To get images from uri
pictureMessageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
行前:
startActivity(pictureMessageIntent);