GVKun编程网logo

Android - 监听传入的 SMS 消息(android监听数据变化)

13

关于Android-监听传入的SMS消息和android监听数据变化的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于AndroidBrowser学习十二传入的Url的处理、AndroidS

关于Android - 监听传入的 SMS 消息android监听数据变化的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android Browser学习十二 传入的Url的处理、Android SMS 短信读取、Android SmsManager、android – Graphhopper,osmdroid for osmdroid离线路由等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

Android - 监听传入的 SMS 消息(android监听数据变化)

Android - 监听传入的 SMS 消息(android监听数据变化)

我正在尝试创建一个应用程序来监控传入的 SMS 消息,并通过传入的 SMS 启动一个程序,它也应该从 SMS 中读取内容。

工作流程:

  • 发送到 Android 设备的短信
  • 自可执行应用程序
  • 阅读短信信息

答案1

小编典典
public class SmsListener extends BroadcastReceiver{    private SharedPreferences preferences;    @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---            SmsMessage[] msgs = null;            String msg_from;            if (bundle != null){                //---retrieve the SMS message received---                try{                    Object[] pdus = (Object[]) bundle.get("pdus");                    msgs = new SmsMessage[pdus.length];                    for(int i=0; i<msgs.length; i++){                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                        msg_from = msgs[i].getOriginatingAddress();                        String msgBody = msgs[i].getMessageBody();                    }                }catch(Exception e){//                            Log.d("Exception caught",e.getMessage());                }            }        }    }}

注意:在您的清单文件中添加 BroadcastReceiver-

<receiver android:name=".listener.SmsListener">    <intent-filter>        <action android:name="android.provider.Telephony.SMS_RECEIVED" />    </intent-filter></receiver>

添加此权限:

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

Android Browser学习十二 传入的Url的处理

Android Browser学习十二 传入的Url的处理

Browser会接收和输入很多url, 有时候用户会输入错误这些url, 于是使用了UrlUtil进行了一些字符串造成, 来做即将输入的浏览器的url的字符串进行处理. 这里用来很多的正则表达式

纠正一些http网址的错误:

 /**
     * 修复一下url的错误
     * @param inUrl
     * @return
     */
    /* package */ static String fixUrl(String inUrl) {
        // FIXME: Converting the url to lower case
        // duplicates functionality in smartUrlFilter().
        // However, changing all current callers of fixUrl to
        // call smartUrlFilter in addition may have unwanted
        // consequences, and is deferred for now.
        int colon = inUrl.indexOf('':'');
        boolean allLower = true;
        for (int index = 0; index < colon; index++) {
            char ch = inUrl.charAt(index);
            if (!Character.isLetter(ch)) {
                break;
            }
            allLower &= Character.isLowerCase(ch);
            if (index == colon - 1 && !allLower) {
                inUrl = inUrl.substring(0, colon).toLowerCase()//前缀变小写
                        + inUrl.substring(colon);
            }
        }
        if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
            return inUrl;
        if (inUrl.startsWith("http:") ||
                inUrl.startsWith("https:")) {
            if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
                inUrl = inUrl.replaceFirst("/", "//");//纠正http:/baidu这样的错误
            } else inUrl = inUrl.replaceFirst(":", "://");//纠正http:baidu这样的错误
        }
        return inUrl;
    }

尝试判断用户在titlebar中输入的是url还是字符串

/**
     * Attempts to determine whether user input is a URL or search
     * terms.  Anything with a space is passed to search if canBeSearch is true.
     *
     * Converts to lowercase any mistakenly uppercased schema (i.e.,
     * "Http://" converts to "http://"
     * 尝试判断用户输入的是一个url还是一个关键词搜索
     *
     * @param canBeSearch If true, will return a search url if it isn''t a valid
     *                    URL. If false, invalid URLs will return null
     * @return Original or modified URL
     *
     */
    public static String smartUrlFilter(String url, boolean canBeSearch) {
        String inUrl = url.trim();
        boolean hasSpace = inUrl.indexOf('' '') != -1;

        Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);//匹配可以识别的uri的正则
        if (matcher.matches()) {
            // force scheme to lowercase 网址的前缀http://等强制小写
     
            String scheme = matcher.group(1);
            String lcScheme = scheme.toLowerCase();
            if (!lcScheme.equals(scheme)) {
                inUrl = lcScheme + matcher.group(2);
            }
            if (hasSpace && Patterns.WEB_URL.matcher(inUrl).matches()) {
                inUrl = inUrl.replace(" ", "%20");//处理空格url为 %20
            }
            return inUrl;
        }
        if (!hasSpace) {
            if (Patterns.WEB_URL.matcher(inUrl).matches()) { //patterns中自带了weburl的正则
                return URLUtil.guessUrl(inUrl);//这是webkit提供的猜测 url的方法
            }
        }
        if (canBeSearch) {//看来不是网址了. 开始使用谷歌进行搜索
            return URLUtil.composeSearchUrl(inUrl,
                    QUICKSEARCH_G, QUERY_PLACE_HOLDER);
        }
        return null;
    }
 static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(//是否是schema前缀
            "(?i)" + // switch on case insensitive matching
            "(" +    // begin group for schema
            "(?:http|https|file):\\/\\/" +
            "|(?:inline|data|about|javascript):" +
            ")" +
            "(.*)" );
 /**
     *  Regular expression pattern to match most part of RFC 3987
     *  Internationalized URLs, aka IRIs.  Commonly used Unicode characters are
     *  added.是否是网址的正则
     */
    public static final Pattern WEB_URL = Pattern.compile(
        "((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\''\\(\\)"
        + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
        + "\\.\\+\\!\\*\\''\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
        + "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+"   // named host
        + TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
        + "|(?:(?:25[0-5]|2[0-4]" // or ip address
        + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
        + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
        + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
        + "|[1-9][0-9]|[0-9])))"
        + "(?:\\:\\d{1,5})?)" // plus option port number
        + "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"  // plus option query params
        + "\\-\\.\\+\\!\\*\\''\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
        + "(?:\\b|$)"); // and finally, a word boundary or end of
                        // input.  This is to stop foo.sure from
                        // matching as foo.su

其拼接关键词比较有意思, 为什么不用String.format呢??

 public static String composeSearchUrl(String inQuery, String template,
                                          String queryPlaceHolder) {
        int placeHolderIndex = template.indexOf(queryPlaceHolder);
        if (placeHolderIndex < 0) {
            return null;
        }

        String query;
        StringBuilder buffer = new StringBuilder();
        buffer.append(template.substring(0, placeHolderIndex));

        try {
            query = java.net.URLEncoder.encode(inQuery, "utf-8");
            buffer.append(query);
        } catch (UnsupportedEncodingException ex) {
            return null;
        }

        buffer.append(template.substring(
                placeHolderIndex + queryPlaceHolder.length()));//防止 %s在 中间出现的情况

        return buffer.toString();
    }

去除http://

 /**
     * 去除 url中的http://等等
     * @param url
     * @return
     */
    public static String stripUrl(String url) {
        if (url == null) return null;
        Matcher m = STRIP_URL_PATTERN.matcher(url);
        if (m.matches()) {
            return m.group(1); //group0 就是那个匹配的http://
        } else {
            return url;
        }
    }
    // Regular expression to strip http:// and optionally
    // the trailing slash
    private static final Pattern STRIP_URL_PATTERN =
            Pattern.compile("^http://(.*?)/?$");

Android自带了很多正则的匹配, 我们不需要自己去找

/**
 * Commonly used regular expression patterns.
 */
public class Patterns {
    /**
     *  Regular expression to match all IANA top-level domains.
     *  List accurate as of 2011/07/18.  List taken from:
     *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
     *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
     */顶级域名
    public static final String TOP_LEVEL_DOMAIN_STR =
        "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
        + "|(biz|b[abdefghijmnorstvwyz])"
        + "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
        + "|d[ejkmoz]"
        + "|(edu|e[cegrstu])"
        + "|f[ijkmor]"
        + "|(gov|g[abdefghilmnpqrstuwy])"
        + "|h[kmnrtu]"
        + "|(info|int|i[delmnoqrst])"
        + "|(jobs|j[emop])"
        + "|k[eghimnprwyz]"
        + "|l[abcikrstuvy]"
        + "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
        + "|(name|net|n[acefgilopruz])"
        + "|(org|om)"
        + "|(pro|p[aefghklmnrstwy])"
        + "|qa"
        + "|r[eosuw]"
        + "|s[abcdeghijklmnortuvyz]"
        + "|(tel|travel|t[cdfghjklmnoprtvwz])"
        + "|u[agksyz]"
        + "|v[aceginu]"
        + "|w[fs]"
        + "|(\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
        + "|y[et]"
        + "|z[amw])";

    /**
     *  Regular expression pattern to match all IANA top-level domains.
     */
    public static final Pattern TOP_LEVEL_DOMAIN =
        Pattern.compile(TOP_LEVEL_DOMAIN_STR);

    /**
     *  Regular expression to match all IANA top-level domains for WEB_URL.
     *  List accurate as of 2011/07/18.  List taken from:
     *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
     *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
     */
    public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
        "(?:"
        + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
        + "|(?:biz|b[abdefghijmnorstvwyz])"
        + "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
        + "|d[ejkmoz]"
        + "|(?:edu|e[cegrstu])"
        + "|f[ijkmor]"
        + "|(?:gov|g[abdefghilmnpqrstuwy])"
        + "|h[kmnrtu]"
        + "|(?:info|int|i[delmnoqrst])"
        + "|(?:jobs|j[emop])"
        + "|k[eghimnprwyz]"
        + "|l[abcikrstuvy]"
        + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
        + "|(?:name|net|n[acefgilopruz])"
        + "|(?:org|om)"
        + "|(?:pro|p[aefghklmnrstwy])"
        + "|qa"
        + "|r[eosuw]"
        + "|s[abcdeghijklmnortuvyz]"
        + "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
        + "|u[agksyz]"
        + "|v[aceginu]"
        + "|w[fs]"
        + "|(?:\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
        + "|y[et]"
        + "|z[amw]))";

    /**
     * Good characters for Internationalized Resource Identifiers (IRI).
     * This comprises most common used Unicode characters allowed in IRI
     * as detailed in RFC 3987.
     * Specifically, those two byte Unicode characters are not included.
     */
    public static final String GOOD_IRI_CHAR =
        "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";

    /**
     *  Regular expression pattern to match most part of RFC 3987
     *  Internationalized URLs, aka IRIs.  Commonly used Unicode characters are
     *  added. 网址 web url 正则
     */
    public static final Pattern WEB_URL = Pattern.compile(
        "((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\''\\(\\)"
        + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
        + "\\.\\+\\!\\*\\''\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
        + "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+"   // named host
        + TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
        + "|(?:(?:25[0-5]|2[0-4]" // or ip address
        + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
        + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
        + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
        + "|[1-9][0-9]|[0-9])))"
        + "(?:\\:\\d{1,5})?)" // plus option port number
        + "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"  // plus option query params
        + "\\-\\.\\+\\!\\*\\''\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
        + "(?:\\b|$)"); // and finally, a word boundary or end of
                        // input.  This is to stop foo.sure from
                        // matching as foo.su
    //IP地址
    public static final Pattern IP_ADDRESS
        = Pattern.compile(
            "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
            + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
            + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
            + "|[1-9][0-9]|[0-9]))");
    //域名
    public static final Pattern DOMAIN_NAME
        = Pattern.compile(
            "(((([" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]*)*[" + GOOD_IRI_CHAR + "]\\.)+"
            + TOP_LEVEL_DOMAIN + ")|"
            + IP_ADDRESS + ")");
    //邮件
    public static final Pattern EMAIL_ADDRESS
        = Pattern.compile(
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
            "\\@" +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
            "(" +
                "\\." +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
            ")+"
        );

    /**
     * This pattern is intended for searching for things that look like they
     * might be phone numbers in arbitrary text, not for validating whether
     * something is in fact a phone number.  It will miss many things that
     * are legitimate phone numbers.
     * 
     * <p> The pattern matches the following:
     * <ul>
     * <li>Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes
     * may follow.
     * <li>Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
     * <li>A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
     * </ul>
     */电话
    public static final Pattern PHONE
        = Pattern.compile(                                  // sdd = space, dot, or dash
                "(\\+[0-9]+[\\- \\.]*)?"                    // +<digits><sdd>*
                + "(\\([0-9]+\\)[\\- \\.]*)?"               // (<digits>)<sdd>*
                + "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit> 

    /**
     *  Convenience method to take all of the non-null matching groups in a
     *  regex Matcher and return them as a concatenated string.
     *
     *  @param matcher      The Matcher object from which grouped text will
     *                      be extracted
     *
     *  @return             A String comprising all of the non-null matched
     *                      groups concatenated together
     */去除指定字符
    public static final String concatGroups(Matcher matcher) {
        StringBuilder b = new StringBuilder();
        final int numGroups = matcher.groupCount();

        for (int i = 1; i <= numGroups; i++) {
            String s = matcher.group(i);

            System.err.println("Group(" + i + ") : " + s);

            if (s != null) {
                b.append(s);
            }
        }

        return b.toString();
    }

    /** 保留 数字和 加号
     * Convenience method to return only the digits and plus signs
     * in the matching string.
     *
     * @param matcher      The Matcher object from which digits and plus will
     *                     be extracted
     *
     * @return             A String comprising all of the digits and plus in
     *                     the match
     */
    public static final String digitsAndPlusOnly(Matcher matcher) {
        StringBuilder buffer = new StringBuilder();
        String matchingRegion = matcher.group();

        for (int i = 0, size = matchingRegion.length(); i < size; i++) {
            char character = matchingRegion.charAt(i);

            if (character == ''+'' || Character.isDigit(character)) {
                buffer.append(character);
            }
        }
        return buffer.toString();
    }

    /**
     * Do not create this static utility class.
     */
    private Patterns() {}
}


Android SMS 短信读取

Android SMS 短信读取

Android SMS Read

源码打印?
  1. package com.homer.sms;  
  2.   
  3. import java.sql.Date;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6.   
  7. import android.app.Activity;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteException;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.widget.ScrollView;  
  14. import android.widget.TableLayout;  
  15. import android.widget.TextView;  
  16.   
  17. /** 
  18.  * 读取手机短信 
  19.  *  
  20.  * @author sunboy_2050 
  21.  * @since  http://blog.csdn.net/sunboy_2050 
  22.  * @date   2012.03.06 
  23.  */  
  24. public class smsRead extends Activity {  
  25.   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.   
  30.         TextView tv = new TextView(this);  
  31.         tv.setText(getSmsInPhone());  
  32.   
  33.         ScrollView sv = new ScrollView(this);  
  34.         sv.addView(tv);  
  35.           
  36.         setContentView(sv);  
  37.     }  
  38.   
  39.     public String getSmsInPhone() {  
  40.         final String SMS_URI_ALL = "content://sms/";  
  41.         final String SMS_URI_INBOX = "content://sms/inbox";  
  42.         final String SMS_URI_SEND = "content://sms/sent";  
  43.         final String SMS_URI_DRAFT = "content://sms/draft";  
  44.         final String SMS_URI_OUTBOX = "content://sms/outbox";  
  45.         final String SMS_URI_FAILED = "content://sms/failed";  
  46.         final String SMS_URI_QUEUED = "content://sms/queued";  
  47.   
  48.         StringBuilder smsBuilder = new StringBuilder();  
  49.   
  50.         try {  
  51.             Uri uri = Uri.parse(SMS_URI_ALL);  
  52.             String[] projection = new String[] { "_id""address""person""body""date""type" };  
  53.             Cursor cur = getContentResolver().query(uri, projection, nullnull"date desc");      // 获取手机内部短信  
  54.   
  55.             if (cur.moveToFirst()) {  
  56.                 int index_Address = cur.getColumnIndex("address");  
  57.                 int index_Person = cur.getColumnIndex("person");  
  58.                 int index_Body = cur.getColumnIndex("body");  
  59.                 int index_Date = cur.getColumnIndex("date");  
  60.                 int index_Type = cur.getColumnIndex("type");  
  61.   
  62.                 do {  
  63.                     String strAddress = cur.getString(index_Address);  
  64.                     int intPerson = cur.getInt(index_Person);  
  65.                     String strbody = cur.getString(index_Body);  
  66.                     long longDate = cur.getLong(index_Date);  
  67.                     int intType = cur.getInt(index_Type);  
  68.   
  69.                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhss");  
  70.                     Date d = new Date(longDate);  
  71.                     String strDate = dateFormat.format(d);  
  72.   
  73.                     String strType = "";  
  74.                     if (intType == 1) {  
  75.                         strType = "接收";  
  76.                     } else if (intType == 2) {  
  77.                         strType = "发送";  
  78.                     } else {  
  79.                         strType = "null";  
  80.                     }  
  81.   
  82.                     smsBuilder.append("[ ");  
  83.                     smsBuilder.append(strAddress + ", ");  
  84.                     smsBuilder.append(intPerson + ", ");  
  85.                     smsBuilder.append(strbody + ", ");  
  86.                     smsBuilder.append(strDate + ", ");  
  87.                     smsBuilder.append(strType);  
  88.                     smsBuilder.append(" ]\n\n");  
  89.                 } while (cur.moveToNext());  
  90.   
  91.                 if (!cur.isClosed()) {  
  92.                     cur.close();  
  93.                     cur = null;  
  94.                 }  
  95.             } else {  
  96.                 smsBuilder.append("no result!");  
  97.             } // end if  
  98.   
  99.             smsBuilder.append("getSmsInPhone has executed!");  
  100.   
  101.         } catch (SQLiteException ex) {  
  102.             Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
  103.         }  
  104.   
  105.         return smsBuilder.toString();  
  106.     }  
  107. }  
package com.homer.sms;

import java.sql.Date;
import java.text.SimpleDateFormat;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;

/**
 * 读取手机短信
 * 
 * @author sunboy_2050
 * @since  http://blog.csdn.net/sunboy_2050
 * @date   2012.03.06
 */
public class smsRead extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TextView tv = new TextView(this);
		tv.setText(getSmsInPhone());

		ScrollView sv = new ScrollView(this);
		sv.addView(tv);
		
		setContentView(sv);
	}

	public String getSmsInPhone() {
		final String SMS_URI_ALL = "content://sms/";
		final String SMS_URI_INBOX = "content://sms/inbox";
		final String SMS_URI_SEND = "content://sms/sent";
		final String SMS_URI_DRAFT = "content://sms/draft";
		final String SMS_URI_OUTBOX = "content://sms/outbox";
		final String SMS_URI_FAILED = "content://sms/failed";
		final String SMS_URI_QUEUED = "content://sms/queued";

		StringBuilder smsBuilder = new StringBuilder();

		try {
			Uri uri = Uri.parse(SMS_URI_ALL);
			String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
			Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");		// 获取手机内部短信

			if (cur.moveToFirst()) {
				int index_Address = cur.getColumnIndex("address");
				int index_Person = cur.getColumnIndex("person");
				int index_Body = cur.getColumnIndex("body");
				int index_Date = cur.getColumnIndex("date");
				int index_Type = cur.getColumnIndex("type");

				do {
					String strAddress = cur.getString(index_Address);
					int intPerson = cur.getInt(index_Person);
					String strbody = cur.getString(index_Body);
					long longDate = cur.getLong(index_Date);
					int intType = cur.getInt(index_Type);

					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhss");
					Date d = new Date(longDate);
					String strDate = dateFormat.format(d);

					String strType = "";
					if (intType == 1) {
						strType = "接收";
					} else if (intType == 2) {
						strType = "发送";
					} else {
						strType = "null";
					}

					smsBuilder.append("[ ");
					smsBuilder.append(strAddress + ", ");
					smsBuilder.append(intPerson + ", ");
					smsBuilder.append(strbody + ", ");
					smsBuilder.append(strDate + ", ");
					smsBuilder.append(strType);
					smsBuilder.append(" ]\n\n");
				} while (cur.moveToNext());

				if (!cur.isClosed()) {
					cur.close();
					cur = null;
				}
			} else {
				smsBuilder.append("no result!");
			} // end if

			smsBuilder.append("getSmsInPhone has executed!");

		} catch (SQLiteException ex) {
			Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
		}

		return smsBuilder.toString();
	}
}

AndroidManifest.xml 权限

 记得在 AndroidManifest.xml 中加入 android.permission.READ_SMS 这个 permission

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


运行结果:


代码示例



URI 主要有:

content://sms/               所有短信
content://sms/inbox        收件箱
content://sms/sent          已发送
content://sms/draft         草稿
content://sms/outbox     发件箱
content://sms/failed       发送失败
content://sms/queued    待发送列表


sms 主要结构:  
  1. _id => 短消息序号 如 100  
  2. thread_id => 对话的序号 如 100  
  3. address => 发件人地址,手机号。如 + 8613811810000  
  4. person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为 null  
  5. date => 日期  long 型。如 1256539465022  
  6. protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO   
  7. read => 是否阅读 0 未读, 1 已读   
  8. status => 状态 -1 接收,0 complete, 64 pending, 128 failed   
  9. type => 类型 1 是接收到的,2 是已发出   
  10. body => 短消息内容   
  11. service_center => 短信服务中心号码编号。如 + 8613800755500  
String[] projection = new String[]{"address", "body"};
Cursor cursor = getContentResolver().query(uri, projection, "where .." new String[]{"", ""}, "order by ..")


Android 短信存储数据库

偶然发现了 Android 源码中的一个类 MmsSmsDatabaseHelper.java,原来 android 将所有的短信信息都存入了 mmssms.db 中。

公开的 SDK 中没有这个类,不能直接使用。于是自己写了一个 SQLiteOpenHelper,但是查询的时候发生 SQL 异常。看来不能为所欲为了,不过据网上资料介绍可以拷贝 db 文件来实现短信数据备份。

MmsSmsDatabaseHelper.java 在 Android 源码中的路径:

packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


sms 数据库中的字段如下:

_id               一个自增字段,从 1 开始
thread_id    序号,同一发信人的 id 相同
address      发件人手机号码
person        联系人列表里的序号,陌生人为 null 
date            发件日期
protocol      协议,分为: 0 SMS_RPOTO, 1 MMS_PROTO  
read           是否阅读 0 未读, 1 已读  
status         状态 -1 接收,0 complete, 64 pending, 128 failed 
type     
    ALL    = 0;
    INBOX  = 1;
    SENT   = 2;
    DRAFT  = 3;
    OUTBOX = 4;
    FAILED = 5;
    QUEUED = 6;
 
body                     短信内容
service_center     短信服务中心号码编号
subject                  短信的主题
reply_path_present     TP-Reply-Path
locked    


sms 数据库表字段类型的源码:

源码打印?
  1. private void createSmsTables(SQLiteDatabase db) {  
  2.         // N.B.: Whenever the columns here are changed, the columns in  
  3.         // {@ref MmsSmsProvider} must be changed to match.  
  4.         db.execSQL("CREATE TABLE sms (" +  
  5.                    "_id INTEGER PRIMARY KEY," +  
  6.                    "thread_id INTEGER," +  
  7.                    "address TEXT," +  
  8.                    "person INTEGER," +  
  9.                    "date INTEGER," +  
  10.                    "date_sent INTEGER DEFAULT 0," +  
  11.                    "protocol INTEGER," +  
  12.                    "read INTEGER DEFAULT 0," +  
  13.                    "status INTEGER DEFAULT -1," + // a TP-Status value  
  14.                                                   // or -1 if it  
  15.                                                   // status hasn''t  
  16.                                                   // been received  
  17.                    "type INTEGER," +  
  18.                    "reply_path_present INTEGER," +  
  19.                    "subject TEXT," +  
  20.                    "body TEXT," +  
  21.                    "service_center TEXT," +  
  22.                    "locked INTEGER DEFAULT 0," +  
  23.                    "error_code INTEGER DEFAULT 0," +  
  24.                    "seen INTEGER DEFAULT 0" +  
  25.                    ");");  
  26. ....  
  27. }  
private void createSmsTables(SQLiteDatabase db) {
        // N.B.: Whenever the columns here are changed, the columns in
        // {@ref MmsSmsProvider} must be changed to match.
        db.execSQL("CREATE TABLE sms (" +
                   "_id INTEGER PRIMARY KEY," +
                   "thread_id INTEGER," +
                   "address TEXT," +
                   "person INTEGER," +
                   "date INTEGER," +
                   "date_sent INTEGER DEFAULT 0," +
                   "protocol INTEGER," +
                   "read INTEGER DEFAULT 0," +
                   "status INTEGER DEFAULT -1," + // a TP-Status value
                                                  // or -1 if it
                                                  // status hasn''t
                                                  // been received
                   "type INTEGER," +
                   "reply_path_present INTEGER," +
                   "subject TEXT," +
                   "body TEXT," +
                   "service_center TEXT," +
                   "locked INTEGER DEFAULT 0," +
                   "error_code INTEGER DEFAULT 0," +
                   "seen INTEGER DEFAULT 0" +
                   ");");
....
}

packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


联系人为空

短信数据库里面如果你是先受到陌生短信之后再把陌生人添加到联系人列表的话,短信数据库里面的 person 字段就为 null,如果你是先添加联系人再发短信的话,短信数据库里面的 person 字段就不为空了,所以你要是想通过短信数据库里的字段取得联系人的其他信息的话,只能通过地址来取。



参考推荐:

Android SMS Messaging 



原文链接: http://blog.csdn.net/sunboy_2050/article/details/7328321

Android SmsManager

Android SmsManager

AndroidManifest.xml 权限

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


SmsManager 类中可以使用的 3 种传送短信的方法:

1.sendDataMessage 发送 Data 格式的 SMS 传送到特定程序的 Port

2.sendMultipartTextMessage 发送多条文字短信

3.sendTextMessage 发送文字短信


android – Graphhopper,osmdroid for osmdroid离线路由

android – Graphhopper,osmdroid for osmdroid离线路由

我一直在使用osmdroid和osm奖励包(令人惊叹的libs!),并且一直在思考离线路由和转向导航的想法,现在,我还没有尝试任何东西,但听起来很的东西有希望投掷graphhopper的混合:

官方Wiki指南如下:

>下载原始地图文件(.osm,.osm.pbf等).
>运行./graphhopper.sh import map_file

现在,这是为了与MapsForge一起使用,我没有机会查看生成的.ghz文件,其中包含地图的路由数据,但我的问题是,是否可以使用此路由数据在OSM Bonus Pack中构建一个PathOverlay对象来实际绘制某种路径?

我想知道是否有人已经尝试了这种类型的东西,并且有任何关于它的说法,任何帮助表示赞赏.

解决方法

在osmdroid中使用Mapsforge:圣杯,永无止境的故事……

你可以看看这个(长)线程:https://groups.google.com/forum/#!topic/osmdroid/gSieR9BF_z0

还有这个工作实验:http://www.salidasoftware.com/how-to-render-mapsforge-tiles-in-osmdroid/

现在,如果您真的需要离线路由,您的选择是:

您将离开osmdroid / OSMBonusPack并转移到Graphhopper / Mapsforge,如Karussell =>所示.你会在Graphhopper网站上找到文档.

或者您可以查看OsmAnd.这是一个应用程序,而不是lib.它有自己的基于矢量的地图格式(和工具集),并提供离线显示和离线路由.

或者你可以尝试达到圣杯,并合并Mapsforge,osmdroid,Graphhopper和OSMBonusPack ……

我们今天的关于Android - 监听传入的 SMS 消息android监听数据变化的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android Browser学习十二 传入的Url的处理、Android SMS 短信读取、Android SmsManager、android – Graphhopper,osmdroid for osmdroid离线路由的相关信息,请在本站查询。

本文标签: