有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓。准许发送短信不工作

我正在尝试将SMS从我的模拟器发送到第二个模拟器

主要问题是安卓系统。准许发送短信可能不起作用

我不确定问题出在哪里,因为我安装了最新的SDK还是什么

我得到以下预期错误,我没有安卓。准许发短信

12-02 21:46:04.150 7881-7881/? W/System: ClassLoader referenced unknown path: /data/app/com.example.alakeel.sms-2/lib/x86_64
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms D/---: Fail
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err: java.lang.SecurityException: Sending SMS message: uid 10057 does not have 安卓.permission.SEND_SMS.
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.os.Parcel.readException(Parcel.java:1599)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.os.Parcel.readException(Parcel.java:1552)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at com.安卓.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at com.example.alakeel.sms.MainActivity.onCreate(MainActivity.java:19)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.Activity.performCreate(Activity.java:6237)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.ActivityThread.-wrap11(ActivityThread.java)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:102)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.os.Looper.loop(Looper.java:148)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)

舱单代码:

<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.example.alakeel.sms">

    <uses-permission 安卓:name="安卓.permission.SEND_SMS"/>

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:supportsRtl="true"
        安卓:theme="@style/AppTheme">
        <activity 安卓:name=".MainActivity">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity类的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("+5554", null, "Bus will be at your door in 30 seconds", null, null);
            Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
            Log.d("---", "Sent");

        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            Log.d("---","Fail");
            e.printStackTrace();
        }

    }
}

共 (1) 个答案

  1. # 1 楼答案

    因为我使用的是API 23,所以我只需将请求权限添加到我的活动中就可以修复它

    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1);