SmsManager sms = SmsManager.getDefault(); ArrayList<String> texts = sms.divideMessage(text); count1 = texts.size(); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(count1); for (int i = 0; i < count1; i++) { sentIntents.add(PendingIntent.getBroadcast(context, 0, success, 0)); } mHandler.postDelayed(new Runnable() { @Override public void run() { try { sms.sendMultipartTextMessage(num,null,texts,sentIntents,null); } catch(Exception ex) { Log.d(TAG,"Error in SendingSms"+ex.getMessage()); } } }, time);我写了一个到达指定时间就发送短信的需求,但是测试时发现插入双卡,就无法发送短信,我该如何解决
时间: 2024-03-27 17:34:56 浏览: 137
这可能是因为你没有指定要使用哪张 SIM 卡来发送短信。你需要使用 `SmsManager` 的 `getDefault()` 方法获取默认的 `SmsManager` 实例,然后调用 `SmsManager` 的 `sendMultipartTextMessage()` 方法时,传入对应的 SIM 卡的 Subscription ID 参数。例如:
```java
int subscriptionId = 1; // 假设要使用第二张 SIM 卡发送短信,Subscription ID 为 1
SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
smsManager.sendMultipartTextMessage(num, null, texts, sentIntents, null);
```
注意,这里的 Subscription ID 需要根据实际情况进行修改。如果是双卡双待手机,可以通过 `TelephonyManager` 的 `getSubId()` 方法获取对应 SIM 卡的 Subscription ID。如果是单卡手机,Subscription ID 默认为 0。
相关问题
修改以下代码public class MainActivity extends AppCompatActivity { private EditText number,content; private Button send; SmsManager smsManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取SmsManager smsManager = SmsManager.getDefault(); number = (EditText) findViewById(R.id.number); content = (EditText) findViewById(R.id.content); send = (Button) findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(),0); Log.i("Main","Pengding"+ pendingIntent); String phone = number.getText().toString(); String msg = content.getText().toString(); smsManager.sendTextMessage(phone,null,msg,pendingIntent,null); Log.i("Main","smsmanager"); Toast.makeText(MainActivity.this,"发送成功",Toast.LENGTH_SHORT); } }); } }
您需要在Toast.makeText()方法中添加.show()才能够显示Toast提示信息,修改后的代码如下:
```
public class MainActivity extends AppCompatActivity {
private EditText number,content;
private Button send;
SmsManager smsManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取SmsManager
smsManager = SmsManager.getDefault();
number = (EditText) findViewById(R.id.number);
content = (EditText) findViewById(R.id.content);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(),0);
Log.i("Main","Pengding"+ pendingIntent);
String phone = number.getText().toString();
String msg = content.getText().toString();
smsManager.sendTextMessage(phone,null,msg,pendingIntent,null);
Log.i("Main","smsmanager");
Toast.makeText(MainActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
}
});
}
}
```
这样就可以在发送短信成功后弹出Toast提示“发送成功”了。
java.lang.SecurityException: Sending SMS message: uid 10134 does not have android.permission.SEND_SMS. at android.os.Parcel.createException(Parcel.java:2071) at android.os.Parcel.readException(Parcel.java:2039) at android.os.Parcel.readException(Parcel.java:1987) at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:1660) at android.telephony.SmsManager$1.onSuccess(SmsManager.java:389) at android.telephony.SmsManager.sendResolverResult(SmsManager.java:1383) at android.telephony.SmsManager.resolveSubscriptionForOperation(SmsManager.java:1351) at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:384) at android.telephony.SmsManager.sendTextMessage(SmsManager.java:357) at com.example.test1.InboxActivity$2.onClick(InboxActivity.java:59) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
根据你提供的信息,这是一个Android应用程序中的异常信息。该异常是由于应用程序尝试发送SMS短信,但该应用程序没有发送短信的权限。在Android应用程序中,需要在AndroidManifest.xml文件中声明应用程序需要的权限,这些权限包括访问硬件设备、使用网络等。在这种情况下,需要在AndroidManifest.xml文件中声明SEND_SMS权限,然后在运行时请求用户授权。
阅读全文