【问题标题】:Android: send sms after receiving oneAndroid:收到一条短信后发送
【发布时间】:2015-06-01 17:55:54
【问题描述】:

我正在编写一个应用程序以在收到 SMS 请求后发送电话位置。接收短信的作品和发送短信的作品。收到短信怎么办?

主活动

final static String gpsLocationProvider = LocationManager.GPS_PROVIDER;
final static String networkLocationProvider = LocationManager.NETWORK_PROVIDER;
static String loc;
public LocationManager locationManager;
static TextView messageBox;


public void sendSMS(String phoneNumber, String message)
{
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    messageBox = (TextView)findViewById(R.id.messageBox);

    locationManager =
            (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    Location lastKnownLocation_byGps =
            locationManager.getLastKnownLocation(gpsLocationProvider);
    Location lastKnownLocation_byNetwork =
            locationManager.getLastKnownLocation(networkLocationProvider);

    if (lastKnownLocation_byGps != null) {
        loc = "gps " + lastKnownLocation_byGps.getLatitude();
    }
    if (lastKnownLocation_byNetwork != null) {
        loc = "net lat:" + lastKnownLocation_byNetwork.getLatitude() + " long:" + lastKnownLocation_byNetwork.getLongitude();
    }
}

public static void updateMessageBox(String msg) {
    messageBox.setText(msg);
}

接收者

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle=intent.getExtras();

    Object[] messages=(Object[])bundle.get("pdus");
    SmsMessage[] sms=new SmsMessage[messages.length];

    for(int n=0;n<messages.length;n++){
        sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    for(SmsMessage msg:sms){
        MainActivity.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
                "Message: "+msg.getMessageBody());
    }
}

sendSMS 方法接收后发送短信放在哪里?

【问题讨论】:

    标签: java android smsmanager


    【解决方案1】:

    您可以在 onReceive 方法的末尾调用您的发送短信代码语句,因为该方法在您收到短信时执行。

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      相关资源
      最近更新 更多