gpt4 book ai didi

java - Android:收到短信后发送短信

转载 作者:行者123 更新时间:2023-12-01 11:29:00 25 4
gpt4 key购买 nike

我编写了一个应用程序来在收到短信请求后发送手机位置。接收短信有效,发送短信也有效。怎样才能让它收到短信后发送短信?

主要 Activity

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方法来接收短信后发送短信?

最佳答案

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

关于java - Android:收到短信后发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30580057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com