Android开发中这些小技巧你都知道吗?(三)

本文介绍了Android开发中的几个实用技巧,包括UrlQuerySanitizer处理URL链接,Fragment设置参数,LocalBroadcastManager实现局部广播,PhoneNumberUtils格式化电话号码,以及如何使用registerActivityLifecycleCallbacks和recreate方法。此外,还提及了Genymotion模拟器和checkSignatures方法用于检查应用签名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


转载请注明出处:http://blog.csdn.net/guxiao1201/article/details/40707815

UrlQuerySanitizer

  • 一个很方便用来处理url链接的工具类,之前开发过程中遇到需要处理支付宝网页url,获取里面post参数,当时使用String的各种接口进行处理,如果用UrlQuerySanitizer的话就简单多了。比如现在有个Url=http://example.com/?name=Mark,我们使用UrlQuerySanitizer拿到name的值:
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer("http://example.com/?name=Mark");
        sanitizer.setAllowUnregisteredParamaters(true);
        String name = sanitizer.getValue("name");

    Fragment public void setArguments (Bundle args)
    Added in  API level 11

    • 在初始化Fragment时向Fragment传参的一个很方便的接口,在Fragment中使用getArguments()来接收。

    Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation.

    LocalBroadcastManager
    • 导入support-v4就可使用LocalBroadcasrManager,和普通的广播相比,LocalBroadcast的范围只是本应内,所以更有效率,更省资源

    PhoneNumberUtils public static String formatNumber (String phoneNumber, String defaultCountryIso)
    Added in  API level 1

    • PhoneNumverUtils提供了一系列方法用来格式化电话号码
    String num = "031185203009";
            PhoneNumberUtils util = new PhoneNumberUtils();
            String numFormated =  util.formatNumber(num,"CN");
    numFormated = 0311-8520-3009

    Breaks the given number down and formats it according to the rules for the country the number is from.

    Parameters
    source The phone number to format
    Returns
    • A locally acceptable formatting of the input, or the raw input if formatting rules aren't known for the number
    Application public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)
    Added in  API level 14
    • 4.0以后新增的一个很方便的回调,callback中有一系列Activity生命周期的方法,例如OnActivityCreated、onActivityDestory和onActivityPaused等。可以在这些方法中一些统筹的逻辑功能,比如统计Activity的使用频率。
    versionNameSuffix
    • 在Gradle脚本中使用该标签可以修改在Manifest中定义的VersionName
    Genymotion
    • 一个比较好用的安卓模拟器,分为免费版、个人版和商业版,其中免费版提供了从2.3到4.4版本的SDK,并带有GPS和摄像头功能。我经常使用这个模拟器做博客Demo的gif图。免费版百度网盘链接:http://pan.baidu.com/s/1kTj2Nu3
    Activity public void recreate ()
    Added in  API level 11

    • 强制一个Activity重新创建自己一个新实例的方法,调用该方法目标Activity会重新走一遍自己的生命周期。

    Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.

    PackageManager public abstract int checkSignatures (String pkg1, String pkg2)
    Added in  API level 1

    • 检查两个apk安装包的签名是否一样,一样的话返回值>0否则返回值<0

    Compare the signatures of two packages to determine if the same signature appears in both of them. If they do contain the same signature, then they are allowed special privileges when working with each other: they can share the same user-id, run instrumentation against each other, etc.

    Parameters
    pkg1 First package name whose signature will be compared.
    pkg2 Second package name whose signature will be compared.
    Returns

    参考资料:

    Android Tips Round-Up, Part 3

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值