Android:在不打开 WhatsApp 应用程序的情况下以编程方式在后台发送带有标题的 WhatsApp 图像:使用辅助功能

问题描述 投票:0回答:0

我创建了一个 Android 应用程序。

我想在后台在 Whatsapp 上发送图片。我开始知道这只能通过辅助功能 API 来完成。

我能够发送文本(感谢:https://stackoverflow.com/a/71804457/5323184), 但为了形象。我做不到

我对图像采用了类似的方法:但它不会自动发送。

 AccessibilityServiceManager serviceManager = new AccessibilityServiceManager(activity.mContext);
            if (serviceManager.hasAccessibilityServicePermission(WhatsAppAccessibilityServices.class)) {
                File appFolderLocation = new File(cachePath, OUTPUT_RENT_RECEIPT_PNG_NAME);
                Uri contentUri = FileProvider.getUriForFile(activity.mContext, BuildConfig.APPLICATION_ID.concat(".provider"), appFolderLocation);

                if (contentUri == null) {
                    In_Place.toastHelper.toastErrorMsg("File Not Present");
                } else {
                    String toNumber = getFormattedNumber(activity.getTenantContactNumber());
                    toNumber = toNumber.replace("+", "").replace(" ", ""); // E164 format without '+' sign

                    Intent sendIntent = new Intent("android.intent.action.MAIN");
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, caption);
                    sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                    sendIntent.setDataAndType(contentUri, activity.mContext.getContentResolver().getType(contentUri));
                    sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net"); //phone number without "+" prefix
                    sendIntent.setPackage(packageName); //com.whatsapp //com.whatsapp.w4b
                    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    activity.mContext.startActivity(sendIntent);
                }
            } else {
                serviceManager.requestUserForAccessibilityService((Activity) activity.mContext);
            }
android whatsapp accessibilityservice
© www.soinside.com 2019 - 2024. All rights reserved.