{"id":1300,"date":"2023-03-18T10:30:45","date_gmt":"2023-03-18T02:30:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1300"},"modified":"2023-04-29T09:17:25","modified_gmt":"2023-04-29T01:17:25","slug":"android-development-security-settings","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-development-security-settings\/","title":{"rendered":"Android\u5f00\u53d1\u5b89\u5168\u8bbe\u7f6e"},"content":{"rendered":"<h2>\u968f\u673a\u6570\u4ea7\u751f\u5668<\/h2>\n<p>\u7981\u7528\uff1a<code>Random<\/code>\uff0c<code>Math.random()<\/code><\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class RandomUtil {\n\n    private static final String digitalChars = &quot;0123456789&quot;;\n\n    public static String generateRandomDigits(int num) {\n        char[] rands = new char[num];\n        for (int i = 0; i &lt; num; i++) {\n            int rand = (int) (Math.random() * 10);\n            rands[i] = digitalChars.charAt(rand);\n        }\n        return new String(rands);\n    }\n\n}<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<code>SecureRandom<\/code><\/p>\n<pre><code class=\"language-java\">public class RandomUtil {\n\n    private static final String SHA1PRNG = &quot;SHA1PRNG&quot;;\n    private static final String digitalChars = &quot;0123456789&quot;;\n\n    public static String generateRandomDigits(int num) {\n        try {\n            SecureRandom sr = SecureRandom.getInstance(SHA1PRNG);\n            char[] rands = new char[num];\n            for (int i = 0; i &lt; num; i++) {\n                int rand = sr.nextInt(10);\n                rands[i] = digitalChars.charAt(rand);\n            }\n            return new String(rands);\n        } catch (NoSuchAlgorithmException e) {\n            NLog.e(Constants.TAG, Log.getStackTraceString(e));\n        }\n        return &quot;&quot;;\n    }\n\n}<\/code><\/pre>\n<h2>\u7981\u6b62\u7528\u6237\u622a\u5c4f<\/h2>\n<p>\u7981\u6b62\u622a\u5c4f\u7684\u5b9e\u73b0\u65b9\u5f0f\u5e76\u4e0d\u662f\u5f88\u96be\uff0c\u5728\u9700\u8981\u8bbe\u7f6e\u7981\u6b62\u622a\u5c4f\u7684 Activity \u7684\u751f\u547d\u5468\u671f<code>onCreate()<\/code>\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4e00\u884c\u4ee3\u7801\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);<\/code><\/pre>\n<h2>\u5806\u6808\u6253\u5370<\/h2>\n<p>\u7981\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">try {\n    ...\n} catch (IOException e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">try {\n    ...\n} catch (IOException e) {\n    NLog.e(Constants.TAG, Log.getStackTraceString(e));\n}<\/code><\/pre>\n<h2>allowBackup\u5c5e\u6027<\/h2>\n<pre><code class=\"language-xml\">&lt;application\n    android:allowBackup=&quot;false&quot;\n    &gt;\n&lt;\/application&gt;<\/code><\/pre>\n<h2>\u5355\u4f8b\u53cc\u91cd\u68c0\u67e5\u4f7f\u7528volatile\u9650\u5236\u7f16\u8bd1\u5668\u91cd\u6392<\/h2>\n<pre><code class=\"language-java\">public class AuthHelper {\n    private static volatile AuthHelper mAuthHelper;\n\n    private AuthHelper(Context context) {\n\n    }\n\n    public static AuthHelper getInstance(Context context) {\n        if (mAuthHelper == null) {\n            synchronized (AuthHelper.class) {\n                if (mAuthHelper == null) {\n                    mAuthHelper = new AuthHelper(context.getApplicationContext());\n                }\n            }\n        }\n        return mAuthHelper;\n    }\n}<\/code><\/pre>\n<h2>\u52a1\u5fc5\u589e\u52a0finally\u4ee3\u7801\u5bf9\u6570\u636e\u6d41\u8fdb\u884c\u5173\u95ed<\/h2>\n<p>\uff081\uff09\u5728finally\u4ee3\u7801\u5bf9\u6570\u636e\u6d41\u8fdb\u884c\u5173\u95ed<\/p>\n<p>\uff082\uff09\u4f7f\u7528<code>try-with-statement<\/code>\u8bed\u6cd5\u7cd6<\/p>\n<p><code>try-with-statement<\/code>\u7528\u6765\u66ff\u4ee3\u7e41\u7410\u7684<code>try-catch-finnally<\/code>\uff0c\u5b83\u4f1a\u81ea\u52a8close\u6240\u6709\u5b9e\u73b0<code>java.lang.AutoCloseable<\/code>\u63a5\u53e3\u7684\u8d44\u6e90<\/p>\n<p>\u5199\u6cd5\u662f\u5728try\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5c0f\u62ec\u53f7\uff0c\u628a\u8d44\u6e90\u7684\u58f0\u660e\u4ee3\u7801\u5199\u8fdb\u53bb\u5373\u53ef<\/p>\n<pre><code class=\"language-java\">try (BufferedReader br = new BufferedReader(new FileReader(path))) {\n    return br.readLine();\n} catch (IOExcepton e) {\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">private static void saveBitmapFile(Bitmap bm, String filePath) throws IOException {\n    File file = new File(filePath);\n    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {\n        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);\n        bos.flush();\n    }\n}<\/code><\/pre>\n<h2>\u7981\u7528MD5\u4f5c\u4e3a\u6458\u8981\u7b97\u6cd5<\/h2>\n<p>\u5f31\u52a0\u5bc6\u6563\u5217\u4e0d\u80fd\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\uff0c\u4e0d\u5e94\u5728\u5b89\u5168\u5173\u952e\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528<\/p>\n<p>\u63a8\u8350\u4f7f\u7528<code>SHA-1<\/code>\u3001<code>SHA-3<\/code>\u3001<code>SHA-224<\/code>\u3001<code>SHA-256<\/code>\u3001<code>SHA-384<\/code>\u3001<code>SHA-512<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u673a\u6570\u4ea7\u751f\u5668 \u7981\u7528\uff1aRandom\uff0cMath.random() public class RandomUtil  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[],"class_list":["post-1300","post","type-post","status-publish","format-standard","hentry","category-android-advance"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1300"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}