Android接收器未在广播中注册SMS

时间:2014-08-08 05:52:41

标签: android sms android-broadcast android-background

我正在开发一个应用程序,我在后台使用以下代码发送短信:

private void sendSMS(String phoneNumber, String randomNumber) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        Intent sentIntent = new Intent(SENT);
        Intent deliveredUntent = new Intent(DELIVERED);

        PendingIntent sentPI = PendingIntent.getBroadcast(MyActivity.this, 0,sentIntent, 0);        
        PendingIntent deliveredPI = PendingIntent.getBroadcast(MyActivity.this, 0,deliveredUntent, 0);

        //---when the SMS has been sent---
        sentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()){
                case Activity.RESULT_OK:
                    Utility.smsFlag = 1;
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Utility.smsFlag = 2;
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Utility.smsFlag = 2;
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Utility.smsFlag = 2;
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Utility.smsFlag = 2;
                    break;
                }
            }
        };

        //---when the SMS has been delivered---
        deliverReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()){
                case Activity.RESULT_OK:
                    break;
                case Activity.RESULT_CANCELED:
                    break;                      
                }
            }
        }; 

        try{
            registerReceiver(sentReceiver, new IntentFilter(SENT));
            registerReceiver(deliverReceiver, new IntentFilter(DELIVERED));
        } catch (Exception e) {
            e.printStackTrace();
        }

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, randomNumber, sentPI, deliveredPI);
    }

使用以下方式接收短信:

public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {     
    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras(); 
    try {             
        if (bundle != null) {                 
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {          
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String senderNumber = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();
                String receiveNumber = message;

                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

                if(Utility.senderNumber.equalsIgnoreCase(Utility.smsSentNumber)){
                    if(Utility.receiveNumber.equalsIgnoreCase(Utility.sentNumber)){
                        abortBroadcast();
                    }
                }                     
            } // end for loop
          } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
}    
}

MPermission

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

广播

<receiver android:name="packagename.smsclass.IncomingSms" >
        <intent-filter android:priority="5000" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

但我收到警告:

08-08 10:55:38.320: W/System.err(14234): java.lang.IllegalArgumentException: Receiver not registered: packeagename.MyActivity$SendSMSInBackground$1@42551540
08-08 10:55:38.320: W/System.err(14234):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
08-08 10:55:38.320: W/System.err(14234):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1297)
08-08 10:55:38.320: W/System.err(14234):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:385)
08-08 10:55:38.325: W/System.err(14234):    at packeagename.MyActivity.callUnregisterReceiver(MyActivity.java:517)
08-08 10:55:38.325: W/System.err(14234):    at packeagename.MyActivity.onPause(MyActivity.java:695)
08-08 10:55:38.325: W/System.err(14234):    at android.app.Activity.performPause(Activity.java:5304)
08-08 10:55:38.325: W/System.err(14234):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1240)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2889)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2858)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2836)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread.access$900(ActivityThread.java:140)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
08-08 10:55:38.325: W/System.err(14234):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 10:55:38.325: W/System.err(14234):    at android.os.Looper.loop(Looper.java:137)
08-08 10:55:38.325: W/System.err(14234):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-08 10:55:38.325: W/System.err(14234):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 10:55:38.325: W/System.err(14234):    at java.lang.reflect.Method.invoke(Method.java:511)
08-08 10:55:38.325: W/System.err(14234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-08 10:55:38.325: W/System.err(14234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-08 10:55:38.325: W/System.err(14234):    at dalvik.system.NativeStart.main(Native Method)

08-08 10:55:38.510: D/dalvikvm(14234): GC_CONCURRENT freed 983K, 16% free 14619K/17287K, paused 12ms+6ms, total 61ms
08-08 10:55:56.970: D/SensorManager(14234): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,).d+a:(
08-08 10:55:57.060: W/System.err(14234): java.lang.IllegalArgumentException: Receiver not registered: packeagename.MyActivity$SendSMSInBackground$1@42551540
08-08 10:55:57.065: W/System.err(14234):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
08-08 10:55:57.065: W/System.err(14234):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1297)
08-08 10:55:57.065: W/System.err(14234):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:385)
08-08 10:55:57.065: W/System.err(14234):    at packeagename.MyActivity.callUnregisterReceiver(SignUpScreenActivity.java:517)
08-08 10:55:57.065: W/System.err(14234):    at packeagename.MyActivity.onDestroy(SignUpScreenActivity.java:702)
08-08 10:55:57.065: W/System.err(14234):    at android.app.Activity.performDestroy(Activity.java:5370)
08-08 10:55:57.065: W/System.err(14234):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1124)
08-08 10:55:57.065: W/System.err(14234):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3324)
08-08 10:55:57.065: W/System.err(14234):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3355)
08-08 10:55:57.065: W/System.err(14234):    at android.app.ActivityThread.access$1300(ActivityThread.java:140)
08-08 10:55:57.065: W/System.err(14234):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
08-08 10:55:57.065: W/System.err(14234):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 10:55:57.070: W/System.err(14234):    at android.os.Looper.loop(Looper.java:137)
08-08 10:55:57.070: W/System.err(14234):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-08 10:55:57.070: W/System.err(14234):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 10:55:57.070: W/System.err(14234):    at java.lang.reflect.Method.invoke(Method.java:511)
08-08 10:55:57.070: W/System.err(14234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-08 10:55:57.070: W/System.err(14234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-08 10:55:57.070: W/System.err(14234):    at dalvik.system.NativeStart.main(Native Method)

请指导我如何删除此内容。

0 个答案:

没有答案