use*_*709 2 java sms android smsmanager
如何发送短信?这看起来很简单,但对我来说不起作用。
我在清单中获得了许可:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.healthapp.healthapp">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application ...
Run Code Online (Sandbox Code Playgroud)
然后我的 onClick 中有这段代码:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个程序时,我得到“不幸的是应用程序已停止”。
以及错误消息:
FATAL EXCEPTION: main
Process: com.healthapp.healthapp, PID: 13477
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
Run Code Online (Sandbox Code Playgroud)
您的代码中有两种不同的发送短信的方法。如果您想使用SmsManager,则不需要Intent/startActivity()方法,该方法会尝试打开另一个应用程序来处理短信。
你可以删除该smsManager.sendTextMessage()行之后的所有内容,然后你就不会Exception再看到它了。
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);
Run Code Online (Sandbox Code Playgroud)