我正在开发发送短信的Android应用程序。我在android 4.3(Jelly bean)上尝试了我的应用程序,它运行得很好。但是当我在Android marshmallow上尝试我的应用程序时,我的应用程序崩溃了。请检查下面的代码
private void sendSMS(String phoneNumber, String message) {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSION_REQUEST_SEND_SMS);
}else{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
我还在AndroidManifest.xml上添加了权限
<uses-permission android:name="android.permission.SEND_SMS"/>
有些文章表示,由于更新的Android版本的许可政策,但即使我添加了许可检查,它仍然无效。在第一次尝试应用程序请求权限,然后我允许它但仍然没有发送短信。在第二次尝试应用程序崩溃。
答案 0 :(得分:0)
希望它能让你感到有帮助......我在我的项目中使用它来自4.0到8.0以上的所有设备
int CurrentSmsParts = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
{
int subscriptionId = SmsManager.getDefaultSmsSubscriptionId();
SmsManager MySmsManager = SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
ArrayList<String> msgArray = MySmsManager.divideMessage(DefaultMsgTemplate);
MySmsManager.sendMultipartTextMessage(phonenumber, null,msgArray, null, null);
CurrentSmsParts = msgArray.size();
Log.d("SMS DETAILS : ", "\nFOUND LOLLIPOP MR1 OR ABOVE...");
Log.d("SMS DETAILS : ", "\nDETECTED DEFAULT SMS SIM..."+subscriptionId);
Log.d("SMS DETAILS : ", "\nSENT MSG USING SIM..."+subscriptionId);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
{
SmsManager MySmsManager = SmsManager.getDefault();
ArrayList<String> msgArray =
MySmsManager.divideMessage(DefaultMsgTemplate);
MySmsManager.sendMultipartTextMessage(phonenumber, null,msgArray, null, null);
CurrentSmsParts = msgArray.size();
Log.d("SMS DETAILS : ", "\nFOUND BELOW LOLLIPOP MR1...");
Log.d("SMS DETAILS : ", "\nDETECTED DEFAULT SMS SIM WHICHEVER AVAILABLE...");
Log.d("SMS DETAILS : ", "\nSENT MSG USING SIM1 OR SIM2 WHICHEVER IS AVAILABLE...");
}
它正在运行代码并在每个仿真器和实际设备上进行测试..希望它有所帮助