通过SmsManager发送短信时应用崩溃

时间:2019-03-28 06:11:42

标签: android android-permissions smsmanager

我正在尝试使用SmsManager向某些设备发送短信,它正在为某些使应用程序崩溃的设备发送短信。我的设备没有崩溃,但在客户端设备上却是崩溃。

我的代码:

 private void sendMessage() {
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendMultipartTextMessage(mobileNumber, null, smsManager.divideMessage(msg), null, null);
        Snackbar.make(findViewById(android.R.id.content), "Message Sent",
                Snackbar.LENGTH_SHORT).show();

    } else {
        String requestPermissionArray[] = {Manifest.permission.SEND_SMS};
        ActivityCompat.requestPermissions(this, requestPermissionArray, PERMISSION_REQUEST_CODE_SEND_SMS);
    }
}

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            switch (requestCode) {
                case PERMISSION_REQUEST_CODE_SEND_SMS:
                    sendMessage();
                    break;
            }
        }
    }

AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />
我在Crashlytics中收到的

崩溃日志

1

Caused by java.lang.SecurityException
   at android.os.Parcel.createException(Parcel.java:1950)
   at android.os.Parcel.readException(Parcel.java:1918)
   at android.os.Parcel.readException(Parcel.java:1868)
   at com.android.internal.telephony.ISms$Stub$Proxy.isImsSmsSupportedForSubscriber(ISms.java:1422)
   at android.telephony.SmsManager.isImsSmsSupported(SmsManager.java:1410)
   at android.telephony.SmsMessage.useCdmaFormatForMoSms(SmsMessage.java:841)
   at android.telephony.SmsMessage.useCdmaFormatForMoSms(SmsMessage.java:827)
   at android.telephony.SmsMessage.fragmentText(SmsMessage.java:373)
   at android.telephony.SmsManager.divideMessage(SmsManager.java:603)
   at in.onlineneta.app.ui.activity.VoterDetailsActivity.sendMessage(VoterDetailsActivity.java:640)
   at in.onlineneta.app.ui.activity.VoterDetailsActivity.onRequestPermissionsResult(VoterDetailsActivity.java:806)
   at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7630)
   at android.app.Activity.dispatchActivityResult(Activity.java:7480)
   at android.app.ActivityThread.deliverResults(ActivityThread.java:4489)
   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4538)
   at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6863)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=2246, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {in.onlineneta.app/in.onlineneta.app.ui.activity.VoterDetailsActivity}: java.lang.SecurityException
   at android.app.ActivityThread.deliverResults(ActivityThread.java:4496)
   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4538)
   at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6863)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:1)

请确保清单

中具有此权限
   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

这是比棒棒糖更大的Android版本代码

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
                int subID = 0;

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
                for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
                    if (subscriptionInfo.getSimSlotIndex() == simID) {
                        subID = subscriptionInfo.getSubscriptionId();
                        break;
                    }
                }
                smsManager = SmsManager.getSmsManagerForSubscriptionId(subID);

                if (textSMS.length() > 160) {
                    smsIntent = getMultipartSMSIntent(smsManager, textSMS, sentIntent, deliveryIntent);
                    smsManager.sendMultipartTextMessage(mobile, null, messageList, smsIntent.getSentIntents(), smsIntent.getDeliveryIntents());
                } else {
                    smsIntent = getSMSIntent(sentIntent, deliveryIntent);
                    smsManager.sendTextMessage(mobile, null, textSMS, smsIntent.getmPendingIntent(), smsIntent.getmDeliveryIntent());
                }
            }