1
我正在制作一个在Android上发送短信的程序。 它工作正常发送,但我有一个litle问题知道什么信息被发送。 以下代码是代码的主要部分: 它从字符串resp中读取许多消息,等待一个随机时间并发送它们中的每一个。但是,我不知道这个SMS的方法onReceive正在确认。获取发送的短信
所以,这是我的问题:我怎么知道很多的短信发送了onReceive确认? 我试图寻找变量和方法两个参数上下文和意图,但它并没有帮助我。
谢谢!
final PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"),0);
registerReceiver(new BroadcastReceiver() {
// executed when the sms is sended
public void onReceive(Context context, Intent intent) {
switch(getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "SMS sended",
Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SMS_SENT"));
// resp is a String with a number and a body by line: <number><body>\n<number><body>\n...
final Scanner scan = new Scanner(resp);
String to;
String body;
SmsManager sms = SmsManager.getDefault();
while (scan.hasNext()){
long r = new Random().nextInt(20000) + 1000;
synchronized(this){
try {
this.wait(r);
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), "ERROR on WAIT",Toast.LENGTH_LONG).show();
}
}
to = scan.next();
body = scan.nextLine();
Toast.makeText(getApplicationContext(), "Sending to " + to,
Toast.LENGTH_LONG).show();
sms.sendTextMessage(to, null, body, pi, null);
}
scan.close();
看起来很酷。我已经有了每个SMS的唯一ID,导致所有消息都在数据库中...所以,我需要为每个SMS发送一个PendingIntent?我会尝试并回来给你一些反馈。谢谢。 –
有什么奇怪的...我需要为每个SMS注册一个_onReceive_?它工作不正常。第一个onReceive被执行,但应用程序“死”之前执行其他人...一些想法?再次感谢你。 –
好吧,我认为这是一个“等待”的问题... –