繁体   English   中英

从Android设备发送短信

[英]Sending a sms from android device

我有一个可以计算自己的位置并将其打印在屏幕上的类。 现在,我想每x次向该地址发送一个具有一定地址的短信。 因此,这需要sleep方法,我想这意味着我需要一个扩展Thread ..的类,但是该类如何从另一个类中获取TextView字符串? 它们将如何相互连接? 顺便说一句,我在这个论坛的某处找到了发送短信的代码:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"+ phoneNumber)));

提前致谢!!

您可以使用SmsManager,

 String Text = "My location is: " +
    "Latitude = " + current_lat +
    "Longitude = " + current_lng;

    SmsManager sender=SmsManager.getDefault();
    sender.sendTextMessage("9762281814",null,Text , null, null);

将与线程相关的内容包装在Async任务中,并向其传递当前活动的Context参数。 这样,您可以在异步任务的doInBackGround()方法内调用Context.findViewById() 这样,您甚至都不会在主线程上执行任何阻塞操作(将来会导致您遇到另一个异常)。

public void SendSmsTask extends AsyncTask<Context, Void, Void> {
  @Override
  protected Void doInBackGround(Context... contexts) {
    for(Context con : contexts) {
      TextView abc = context.findViewById(<textview Id>);
      // send your sms after this
    }
  }
... // remaining functions and logic
}

您可以使用以下活动从活动中启动此任务:

new SendSmsTask().execute(this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM