如何prevent短信会在Android的收件箱? [英] How to prevent SMS going to inbox in Android?

查看:162
本文介绍了如何prevent短信会在Android的收件箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个企业短信的应用程序。在这个程序中,如果传入消息是从一个特定的号码,说999999999,就应该到应用程序的收件箱,而不是默认的本地收件箱。所有其它消息应该到手机内置的收件箱。如何做到这一点?

I'm developing a business SMS application. In this app, if an incoming message is from a particular number, say 999999999, it should go to the application's inbox and not to the default native inbox. All other messages should go to the phone's native inbox. How do I do this?

推荐答案

当接收到Android系统的手机短信,这是广播的有序广播 意图用行动android.provider.Telephony.SMS_RECEIVED。所有注册的接收器,包括系统默认的短信应用,收到此意图,以便优先级的那摆在他们的意图过滤器。具有相同优先级广播receirers的顺序是不确定的。任何的BroadcastReceiver 可以prevent使用接收到广播的其他注册的广播接收器 abortBroadcast()

When SMS is received by the Android system, it broadcasts an ordered broadcast Intent with action "android.provider.Telephony.SMS_RECEIVED". All registered receivers, including the system default SMS application, receive this Intent in order of priority that was set in their intent-filter. The order for broadcast receirers with the same priority is unspecified. Any BroadcastReceiver could prevent any other registered broadcast receivers from receiving the broadcast using abortBroadcast().

所以,这样你需要的一切是广播接收器:

So, everything you need is broadcast receiver like this:

public class SmsFilter extends BroadcastReceiver {

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

        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                Object[] pdus = (Object[])extras.get("pdus");

                if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.

                StringBuilder sb = new StringBuilder();
                String sender = null;
                for (int i = 0; i < pdus.length; i++) {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    if (sender == null) sender = message.getOriginatingAddress();
                    String text = message.getMessageBody();
                    if (text != null) sb.append(text);
                }
                if (sender != null && sender.equals("999999999")) {
                    // Process our sms...
                    abortBroadcast();
                }
                return;
            }
        }

        // ...
    }
}

貌似系统默认的短信处理程序使用 0 的优先级,所以你可以尝试 1 为您的应用是前。这些行添加到您的的Andr​​oidManifest.xml

Looks like the system default SMS processing application uses priority of 0, so you could try 1 for your application to be before it. Add these lines to your AndroidManifest.xml:

<receiver android:name=".SmsFilter">
    <intent-filter android:priority="1">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

不要忘了必要的权限:

Don't forget about necessary permissions:

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

顺便说一句,你可以找到所有已注册的接收器,并使用该code优先级:

By the way, you can find all registered receivers and their priorities using this code:

Intent smsRecvIntent = new Intent("android.provider.Telephony.SMS_RECEIVED");
List<ResolveInfo> infos = context.getPackageManager().queryBroadcastReceivers(smsRecvIntent, 0);
for (ResolveInfo info : infos) {
    System.out.println("Receiver: " + info.activityInfo.name + ", priority=" + info.priority);
}

更新:作为FantasticJamieBurn 说下面,从Android 4.4系统的唯一的应用程序开始,可以拦截短信(如果它希望块)是默认的短信应用程序(由用户选择)。所有其他应用程序只能听传入的短信,如果默认短信应用程序不会被阻止了。

Update: As FantasticJamieBurn said below, starting from Android 4.4 the only app that can intercept SMS (and block if it wish) is the default SMS app (selected by user). All other apps can only listen for incoming SMS if default SMS app not blocked it.

参见短信提供商在Android 4.4的API。

See also SMS Provider in the Android 4.4 APIs.

这篇关于如何prevent短信会在Android的收件箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆