gpt4 book ai didi

android - 如何根据条件将 SMS 传递到默认 SMS 应用程序

转载 作者:行者123 更新时间:2023-11-30 03:15:23 25 4
gpt4 key购买 nike

我正在寻找一个应用程序来检查设备的速度(如果是驾驶等),如果它低于预设的阈值,它会通过标准通知将 SMS 传递到标准接收设备。如果标准失败(移动太快)。我希望它仍然传递 SMS,抑制通知,并自动向发件人发送回复。

目前,这是我用于接收和发送的内容:

    package biz.midl.drivereply;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class MainActivity extends BroadcastReceiver {

int MAX_SPEED = 1000; //will have a change method later

@Override
public void onReceive(Context arg0, Intent arg1) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //context cannot be resolved
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (lastKnownLocation.getSpeed() > MAX_SPEED)
{
Bundle extras = intent.getExtras(); //intent cannot be resolved

if (extras == null)
{
return;
}

abortBroadcast();

Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);

String origNumber = msg.getOriginatingAddress();
String reply = "The user is busy. Try again later.";

SmsManager.getDefault().sendTextMessage(origNumber, null, reply, null, null);
}
}
}

我在带有注释的行之后显示了错误。

最佳答案

由于您只对接收 SMS 时的速度感兴趣,因此无需持续监控您的位置和速度。在这种情况下,您的 BroadcastReceiver 应该实现为在 SMS_RECEIVED 广播时启动。为此,请在您的 list 中注册您的接收器,如下所示:

<receiver android:name=".SMSReceiver"> 
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

然后,在 onReceive() 方法中,简单地检查最后一个已知位置的速度,并在必要时回复:

@Override
public void onReceive(Context context, Intent intent)
{
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (lastKnownLocation.getSpeed() > MAX_SPEED)
{
Bundle extras = intent.getExtras();

if (extras == null)
{
return;
}

abortBroadcast();

Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);

String origNumber = msg.getOriginatingAddress();
String reply = "The user is busy. Try again later.";

SmsManager.getDefault().sendTextMessage(origNumber, null, reply, null, null);
}
}

在上面的示例中,接收器的优先级设置为最大值 (999),因此它将首先接收 SMS_RECEIVED 广播。然后,如果速度大于您定义的速度限制,广播将中止,并向发件人发送回复。否则,什么都不做,广播将继续发送给其他已注册接收它的接收者,例如平台 SMS 应用程序。

关于android - 如何根据条件将 SMS 传递到默认 SMS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218819/

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