0

我有一个监听传入消息的应用程序,如果原始发件人是用户指定的发件人,它会做出相应的反应,显示特殊警报并中止广播,防止其到达收件箱。在 Verizon 上,它运行良好。我已经发送了 300 多个,没有任何问题,其他一些测试人员也是如此。

但是,在任何其他运营商上,这都是一团糟。

在 AT&T 上,广播永远不会中止,它会显示在短信收件箱中。在 Sprint 上,广播被中止,但它永远不会超出此范围。永远不会调用 AlertActivity 意图,也不会调用我要检查的任何 toast 消息。在 T-Mobile 上,广播永远不会中止,它会显示在短信收件箱中。

我在 java 中完成了接收器,而不是在 Manifest 中注册,因为我在应用程序启动和 BOOT_COMPLETED 启动的服务中注册了它。

服务

   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    public void startService() {
        IntentFilter SMSfilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        this.registerReceiver(Receiver.br, SMSfilter);
    }

接收者

static public BroadcastReceiver br = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                if (messages[i].getOriginatingAddress().equals(Test.SENDER)) {
                    abortBroadcast();
                    String[] body = messages[i].getDisplayMessageBody().split(" ", 7);
                    if (body[0].equals("test")) {
                        test = true;
                    }
                    cat = body[1];
                    level = body[2];
                    urgency = body[3];
                    certainty = body[4];
                    carrier = body[5];
                    message = body[6];
                    intent = new Intent(context, AlertActivity.class);
                    Bundle b = new Bundle();
                    b.putString("title", cat);
                    b.putString("certainty", certainty);
                    b.putString("urgency", urgency);
                    b.putString("level", level);
                    b.putString("message", message);
                    b.putBoolean("test", test);
                    intent.putExtras(b);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                           TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                    carrierName = manager.getNetworkOperatorName();
                    if (carrierName.replaceAll(" ", "").equals(carrier)) {
                        context.startActivity(intent);
                    } else {
                        //testing
                        toast(carrierName.replaceAll(" ", ""), context);
                    }
                }
            }
    }
    }
};

我在应用程序中使用这些导入,

import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;

我知道这些也有一个 gsm 版本,我不使用它。这可能是应用程序没有检测到 gsm 运营商上的传入消息的原因吗?

更新 1 根据http://developer.android.com/reference/android/telephony/gsm/package-summary.html这不是因为没有使用 gsm 特定的进口。

回答 明白了。它与如何读取传入的消息发件人号码有关。在 verizon 设备上,它会在其他设备上注册为 xxxxxxx,+1xxxxxxx。添加了访问 Test.SENDER 或 Test.SENDER_LAME 的选项,即 +1xxxxxxx

4

1 回答 1

0

知道了。它与如何读取传入的消息发件人号码有关。在 verizon 设备上,它会在其他设备上注册为 xxxxxxx,+1xxxxxxx。添加了访问 Test.SENDER 或 Test.SENDER_LAME 的选项,即 +1xxxxxxx

于 2012-03-16T04:30:42.723 回答