java - Android android.permission.SEND_SMS 不工作

标签 java android sms

<分区>

我正在尝试从我的模拟器向第二个模拟器发送短信

主要问题是 Android android.permission.SEND_SMS 可能无法正常工作。

我不确定是因为我安装了最新的 SDK 还是什么问题?

我收到以下预期错误,我没有 android.permission.SEND_SMS。

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 android.permission.SEND_SMS.
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.os.Parcel.readException(Parcel.java:1599)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.os.Parcel.readException(Parcel.java:1552)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at com.android.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 android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.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 android.app.Activity.performCreate(Activity.java:6237)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-02 21:46:04.240 7881-7881/com.example.alakeel.sms W/System.err:     at android.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.android.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.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

list 代码:

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

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

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

                <category android:name="android.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();
        }

    }
}

最佳答案

因为我使用的是 API 23,我只是通过将请求权限添加到我的 Activity 中来修复它

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

关于java - Android android.permission.SEND_SMS 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057030/

相关文章:

java - 如何使用 HttpURLConnection 检查 https

java - openshift 中 Spring Boot/VertX 调用超时

java - 检索 TextView.getText() 以设置带有开始按钮的 CountDownTimer

android - Android中BroadcastReceiver自动读取短信

sms - 寻找可以在短信中发送的有效字符列表

java - 以百分比设置按钮的大小

java - 通过 Intent 传递 Youtube 视频 ID

java - 在 RxJava 中使用 else 语句

java - 如何在android studio中正确安装jdbc驱动?

java - 如何设置 SMS 服务器?