gpt4 book ai didi

android - BroadcastReceiver 在 BOOT_COMPLETED 后不工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:35 24 4
gpt4 key购买 nike

我试图在 boot_completed 后立即拦截传入的 SMS,但我在这一行中遇到 NullPointerException 问题:

Object[] rawMsgs=(Object[])intent.getExtras().get("pdus");

这是我的 list :

    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses- permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
<receiver android:name=".SMSReceiver"
android:permission="android.permission.BROADCAST_SMS"
>
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>

接收者:

public class SMSReceiver extends BroadcastReceiver 
{


private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {


}

public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

@Override
public void onReceive(Context context, Intent intent) {


LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
//Get as fine grained location as possible, while saving battery life.
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = lm.getBestProvider(criteria, true);
Toast toast3 = Toast.makeText(context, "Provider: "+provider, Toast.LENGTH_LONG);
toast3.show();

Object[] rawMsgs=(Object[])intent.getExtras().get("pdus");

for (Object raw : rawMsgs) {
SmsMessage msg=SmsMessage.createFromPdu((byte[])raw);
SmsManager sms = SmsManager.getDefault();
if (msg.getMessageBody().toUpperCase().contains("LOC")) {
Log.w("SMS:"+msg.getOriginatingAddress(),
msg.getMessageBody());
Toast toast1 = Toast.makeText(context, "Phone Number: "+msg.getOriginatingAddress()+" Message: "+msg.getMessageBody(), Toast.LENGTH_LONG);
toast1.show();
abortBroadcast();

if(provider!=null){


lm.requestLocationUpdates(provider, 0, 0, locationListener);

if(lm != null)
{

Location last_good = lm.getLastKnownLocation(provider);
if(last_good != null)
{
Toast.makeText(context, "Got Message from " + msg.getOriginatingAddress() + " , Sending" + last_good.toString(), Toast.LENGTH_SHORT).show();
//sendSMS(msg.getOriginatingAddress(), "http://maps.google.com?q=" + last_good.convert(last_good.getLatitude(), Location.FORMAT_DEGREES) + "," + last_good.convert(last_good.getLongitude(), Location.FORMAT_DEGREES), context);

//sendSMS(msg.getOriginatingAddress(), last_good.getLatitude() + "," + last_good.getLongitude(), context);
sms.sendTextMessage(msg.getOriginatingAddress(), null, last_good.getLatitude() + "," + last_good.getLongitude(), null, null);
lm.removeUpdates(locationListener);
}
else
{
lm.removeUpdates(locationListener);
sendSMS(msg.getOriginatingAddress(),"Location Not Available. Possible Reasons: Phone is Off, GPS is Off, No Satellites in sight", context);

}
}

}
else{
sendSMS(msg.getOriginatingAddress(),"Location Not Available. Possible Reasons: Phone is Off, GPS is Off, No Satellites in sight", context);

}



}
}
}

谢谢。

最佳答案

这里的问题是您的 BroadcastReceiver 实现已映射到两个 Intent - android.provider.Telephony.SMS_RECEIVEDandroid.intent.action.BOOT_COMPLETED - 但在onReceive 实现你没有检查你正在处理哪个 Intent 。

我的猜测android.intent.action.BOOT_COMPLETED 已经收到并且 intent.getExtras() 正在返回 null。您可以通过向该方法添加一些日志记录并查看 logcat 窗口(如果您使用的是 Eclipse)来确认这一点。

如果您编写服务或广播接收器并将其映射到多个 Intent ,这是一个很好的做法 - 不重要 - 检查已接收到哪个 Intent 并适本地处理它。就我个人而言,我会选择这样的东西:

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// boot-related processing here
}
else if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
// SMS-related processing here
}
else {
// intent not handled - log as a warning as you've registered to receive it
}

您不能假设故障安全行为。为清楚起见,将 Intent 处理逻辑移至单独的方法中:

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
processBootCompletedIntent(intent);
}
else if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
processSmsReceivedIntent(intent);
}
else {
// intent not handled - log as a warning as you've registered to receive it
}

PS - 大多数(如果不是全部)“ native ” Intent 操作字符串在相关类中作为常量保存。例如 android.intent.action.BOOT_COMPLETED 被定义为 Intent.ACTION_BOOT_COMPLETED。在常量存在的地方使用它们,它会避免您犯任何键入错误。

关于android - BroadcastReceiver 在 BOOT_COMPLETED 后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6878816/

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