BroadcastReceiver的BOOT

编程入门 行业动态 更新时间:2024-10-26 00:18:51
本文介绍了BroadcastReceiver的BOOT_COMPLETED后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图拦截来袭短信右boot_completed之后,但我在使用这一行NullPointerException异常的问题:

I'm trying to intercept incoming SMS right after boot_completed but i'm having a problem with a NullPointerException in this line:

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

下面是我的清单:                   

Here's my manifest:

<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(), "maps.google?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实现已经映射到两个目的 - android.provider.Telephony.SMS_RECEIVED 和 android.intent.action.BOOT_COMPLETED - 但在执行的onReceive你不检查其意图正在处理

The problem here is that your BroadcastReceiver implementation has been mapped to two intents - android.provider.Telephony.SMS_RECEIVED and android.intent.action.BOOT_COMPLETED - but in the onReceive implementation you're not checking which intent you are processing.

我的的猜测的是 android.intent.action.BOOT_COMPLETED 已收到, intent.getExtras()则返回null。你可以通过添加一些日志记录的方法,看的logcat窗口(如果你使用Eclipse)证实了这一点。

My guess is that android.intent.action.BOOT_COMPLETED has been received and intent.getExtras() is returning null. You could confirm this by adding some logging to the method and watching the logcat window (if you're using Eclipse).

如果你写了一个服务或广播接收器,并将其映射到多个意图是很好的做法 - 不必需的 - 要检查其意图已收到,并适当地处理它。个人而言,我会去是这样的:

If you write a service or broadcast receiver and map it to multiple intents it is good practice - nay essential - to check which intent has been received and process it appropriately. Personally I'd go for something like this:

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 }

您可以不承担故障安全的行为。为了清楚起见,将意图处理逻辑到一个单独的方法:

You can't assume fail-safe behaviour. For clarity, move the intent processing logic into a separate method:

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 - 大部分(如果不是全部)'本土'的意图操作字符串保持在一个相关类的常量。例如android.intent.action.BOOT_COMPLETED被定义为Intent.ACTION_BOOT_COMPLETED。使用如存在常数,它可以节省你做任何输入错误。

PS - most (if not all) 'native' intent action strings are held as constants in a relevant class. For example android.intent.action.BOOT_COMPLETED is defined as Intent.ACTION_BOOT_COMPLETED. Use the constants where they exist, it'll save you making any typing errors.

更多推荐

BroadcastReceiver的BOOT

本文发布于:2023-11-27 10:32:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BroadcastReceiver   BOOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!