[英]Android all variables are reset when SMS is sent
嘿,我正在制作一個需要發送短信的應用程序,但是每次發送該消息時,該應用程序都會再次打開並且所有變量都將重置(我嘗試實現一個保存變量但仍會重置的系統),但是它仍然發送消息。 為什么這樣做,我該如何解決? 這是我的代碼
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);
}
});
您要求SMSManager在SMS成功發送后重新啟動您的應用程序。
從文檔中 ,
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent),
代碼中的pi
將用作sentIntent
,這意味着當SMS從設備中發送出去時, SMSManager
將自動觸發該intent。
如果您不希望SMS管理器在發送SMS后再次重新啟動您的應用程序,只需發送null
代替pi
。
sm.sendTextMessage(phono, null, mes, null, null);
替換sm.sendTextMessage(phono, null, mes, pi, null);
與sm.sendTextMessage(phono, null, mes, null, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.