我正在编写一个 android 应用程序(带有服务),当我收到 GCM 通知时我需要发送一条消息。
主要问题:当我同时发送3条短信时,任何其他操作都会长时间停止(10-20秒)并且手机也被阻止。我尝试在线程中发送短信,但没有区别。
短信发送器.java
public class SmsSender implements Runnable{
private Context context;
private String phoneNumber;
private String message;
private int id;
public SmsSender(Context paramContext, String phoneNumber, String message, int id) {
super();
this.context = paramContext;
this.phoneNumber = phoneNumber;
this.message = message;
this.id = id;
}
public void run() {
try {
Intent sentIntent = new Intent("sent");
Intent deliveredIntent = new Intent("delivered");
sentIntent.putExtra("id", id);
deliveredIntent.putExtra("id", id);
PendingIntent sentPI = PendingIntent.getBroadcast(this.context, id,sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this.context, id, deliveredIntent, 0);
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
sentList.add(sentPI);
ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
deliveredList.add(deliveredPI);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, sentList, deliveredList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行线程
新线程(新 SmsSender(getApplicationContext(),phoneNumber,消息,id)).start();
问题出在哪里?为什么短信是在主线程而不是我的线程中发送的?