嘿,我正在制作一个应用程序,我需要发送一条短信,但是每次我发送消息时,应用程序再次打开,所有变量都被重置(我已经尝试实现一个系统,保存变量,但它们仍然被重置),但是它仍然发送消息。它为什么要这样做,以及如何修复它;这是我的代码。
public void sendSMS(String phono, String mes)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phono, null, mes, pi, null);
}
//Button that uses method
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
phono = "personal phone number";
if (phono.length() > 0 && mes.length() > 0)
sendSMS(phono, mes);
}
});发布于 2015-07-15 03:55:24
当短信成功发送时,您要求SMSManager重新启动您的应用程序。
从文档,
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent),您的代码中的pi将用作sentIntent,这意味着当SMS被发送出设备时,SMSManager将自动触发意图。
如果你不希望短信管理器在短信发送后再次重新启动你的应用程序,只需发送一个null而不是pi。
sm.sendTextMessage(phono, null, mes, null, null);发布于 2015-07-15 04:01:10
将sm.sendTextMessage(phono, null, mes, pi, null);替换为sm.sendTextMessage(phono, null, mes, null, null);
https://stackoverflow.com/questions/31421128
复制相似问题