如何使用具有相同Intent-filter的两个不同Apps的两个不同BroadcastReciver?

时间:2015-09-24 06:50:55

标签: android android-intent broadcastreceiver sms

我有两个不同的应用程序。它们都在BroadcastReceiver中注册了AndroidManifest.xml个类intent-filterintent-filterandroid.provider.Telephony.SMS_RECEIVED。两个应用程序都会收听传入的短信,并根据短信进行操作。现在我的问题是只安装了一个应用程序。如果我在短信收到一个应用程序崩溃时在我的手机中安装了这两个应用程序。如果我强制停止任何人,那么其他应用程序工作正常。如何使用具有相同Intent过滤器的两个不同的BroadcastRecivers两个不同的应用程序? 请提出你的建议。感谢。

Stack Trace是

09-24 12:58:13.549  19882-19882/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.realtech.securetracknew, PID: 19882
java.lang.RuntimeException: Unable to start receiver com.realtech.securetracknew.LogReceiver: java.lang.NullPointerException
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
        at android.app.ActivityThread.access$1700(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.realtech.securetracknew.LogReceiver.onReceive(Unknown Source)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)

at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)

我的onReceive方法是

    @Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(smsBundle);
            for (int i = 0; i < sms.length; i++) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
                smsBody = smsMessage.getMessageBody();
                smsAddress = smsMessage.getOriginatingAddress();
                try {
                    smsFrom = smsAddress.substring(3, 13);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            Toast.makeText(context,smsBody,Toast.LENGTH_LONG).show();
        }
    }
}

0 个答案:

没有答案