gpt4 book ai didi

java - 无法以编程方式发送 WhatsApp 消息

转载 作者:行者123 更新时间:2023-12-01 12:24:31 26 4
gpt4 key购买 nike

我正在尝试使用以下代码以编程方式发送 WhatsApp 消息。

void openWhatsappContact(String number, Context context) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
i.putExtra(Intent.EXTRA_TEXT, "test");
context.startActivity(Intent.createChooser(i, ""));
}

这个辅助函数每次都会成功调用(我通过记录它们来检查),但无法发送消息。显然,在 startActivity 时一切都出了问题(从日志来看)。贴出日志供引用。

10-20 15:59:34.029: W/System.err(16336): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
10-20 15:59:34.029: W/System.err(16336): at android.app.ContextImpl.startActivity(ContextImpl.java:847)
10-20 15:59:34.029: W/System.err(16336): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
10-20 15:59:34.029: W/System.err(16336): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
10-20 15:59:34.029: W/System.err(16336): at com.scimet.admin.driveon.RejectCall.openWhatsappContact(RejectCall.java:79)
10-20 15:59:34.029: W/System.err(16336): at com.scimet.admin.driveon.RejectCall.onReceive(RejectCall.java:63)
10-20 15:59:34.029: W/System.err(16336): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2139)
10-20 15:59:34.029: W/System.err(16336): at android.app.ActivityThread.access$1500(ActivityThread.java:127)
10-20 15:59:34.029: W/System.err(16336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
10-20 15:59:34.029: W/System.err(16336): at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 15:59:34.029: W/System.err(16336): at android.os.Looper.loop(Looper.java:137)
10-20 15:59:34.029: W/System.err(16336): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-20 15:59:34.029: W/System.err(16336): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 15:59:34.029: W/System.err(16336): at java.lang.reflect.Method.invoke(Method.java:511)
10-20 15:59:34.029: W/System.err(16336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-20 15:59:34.029: W/System.err(16336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-20 15:59:34.029: W/System.err(16336): at dalvik.system.NativeStart.main(Native Method)

最佳答案

添加-

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

关于java - 无法以编程方式发送 WhatsApp 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26464331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com