fra*_*ndo 6 android android-intent android-softkeyboard android-input-method
我正在尝试实现一个键盘应用程序,它应该能够将图像发送到当前活动(Whatsapp,消息应用程序等).
有没有办法真正实现这一目标?当然它仅限于接受图像的应用程序,但我想知道什么是最好的方法.
尝试使用带有ImageSpan的StringBuilder但无法使其工作.我想知道是否有更好的方法.使用Intents可能吗?
最终通过将意图发送到前台应用程序来实现这一目标,但这有局限性:消息传递应用程序通常需要选择对话,这会破坏用户流程并添加不必要的步骤(除非它们公开了一种将意图发送到特定聊天的方法) 。
这可以通过以下方式实现:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setPackage(getCurrentAppPackage(context, editorInfo));
return sendIntent;
Run Code Online (Sandbox Code Playgroud)
其中是 给定 a和 angetCurrentAppPackage(...)返回前台 Activity 的方法,当绑定到输入字段时,您可以从 IME 实现中获取该方法。ContextEditorInfo
public String getCurrentAppPackage(Context context, EditorInfo info) {
if(info != null && info.packageName != null) {
return info.packageName;
}
final PackageManager pm = context.getPackageManager();
//Get the Activity Manager Object
ActivityManager aManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//Get the list of running Applications
List<ActivityManager.RunningAppProcessInfo> rapInfoList = aManager.getRunningAppProcesses();
//Iterate all running apps to get their details
for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
//error getting package name for this process so move on
if (rapInfo.pkgList.length == 0) {
Log.i("DISCARDED PACKAGE", rapInfo.processName);
continue;
}
try {
PackageInfo pkgInfo = pm.getPackageInfo(rapInfo.pkgList[0], PackageManager.GET_ACTIVITIES);
return pkgInfo.packageName;
} catch (PackageManager.NameNotFoundException e) {
// Keep iterating
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
更新:提交内容 API 已添加到 API 级别 25(并且支持库使其可以从 API 13 开始工作)。更多信息请参见: https: //developer.android.com/preview/image-keyboard.html 在应用程序开始实现它之前,上述方法可以用作后备。
| 归档时间: |
|
| 查看次数: |
1511 次 |
| 最近记录: |