{"id":175171,"date":"2024-11-08T03:38:27","date_gmt":"2024-11-07T19:38:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175171\/"},"modified":"2024-11-08T03:38:28","modified_gmt":"2024-11-07T19:38:28","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8android%e5%88%a4%e6%96%b7%e7%b6%b2%e7%b5%a1%e9%a1%9e%e5%9e%8b%e4%b8%a6%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-android%e5%88%a4%e6%96%b7%e4%bb%80%e9%ba%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175171\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Android\u5224\u65b7\u7db2\u7d61\u985e\u578b\u4e26\u9023\u63a5\u6578\u64da\u5eab (android\u5224\u65b7\u4ec0\u9ebc\u7db2\u7d61\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8android%e5%88%a4%e6%96%b7%e7%b6%b2%e7%b5%a1%e9%a1%9e%e5%9e%8b%e4%b8%a6%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-zYXuwBFxna\">\u5982\u4f55\u4f7f\u7528Android\u5224\u65b7\u7db2\u7d61\u985e\u578b\u4e26\u9023\u63a5\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u7db2\u7d61\u9023\u63a5\u7684\u7a69\u5b9a\u6027\u548c\u985e\u578b\u5c0d\u65bc\u61c9\u7528\u7684\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002Android\u5e73\u53f0\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5224\u65b7\u7576\u524d\u7684\u7db2\u7d61\u985e\u578b\uff0c\u4e26\u6839\u64da\u7db2\u7d61\u72c0\u614b\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Android\u61c9\u7528\u4e2d\u5224\u65b7\u7db2\u7d61\u985e\u578b\uff0c\u4e26\u793a\u7bc4\u5982\u4f55\u6839\u64da\u4e0d\u540c\u7684\u7db2\u7d61\u72c0\u614b\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e5%88%a4%e6%96%b7%e7%b6%b2%e7%b5%a1%e9%a1%9e%e5%9e%8b-zYXuwBFxna\">\u5224\u65b7\u7db2\u7d61\u985e\u578b<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528ConnectivityManager\u4f86\u7372\u53d6\u7576\u524d\u7684\u7db2\u7d61\u72c0\u614b\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>import android.content.Context;\nimport android.net.ConnectivityManager;\nimport android.net.NetworkInfo;\n\npublic class NetworkUtil {\n    public static String getNetworkType(Context context) {\n        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);\n        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();\n        if (activeNetwork != null) {\n            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {\n                return \"WiFi\";\n            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {\n                return \"Mobile Data\";\n            }\n        }\n        return \"No Connection\";\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u7372\u53d6ConnectivityManager\u7684\u5be6\u4f8b\uff0c\u7136\u5f8c\u6aa2\u67e5\u7576\u524d\u7684\u7db2\u7d61\u72c0\u614b\u3002\u5982\u679c\u7576\u524d\u7684\u7db2\u7d61\u662fWiFi\u6216\u79fb\u52d5\u6578\u64da\uff0c\u6211\u5011\u5c07\u8fd4\u56de\u76f8\u61c9\u7684\u5b57\u7b26\u4e32\uff0c\u5426\u5247\u8fd4\u56de\u201c\u7121\u9023\u63a5\u201d\u3002<\/p>\n<h2 id=\"%e6%a0%b9%e6%93%9a%e7%b6%b2%e7%b5%a1%e9%a1%9e%e5%9e%8b%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-zYXuwBFxna\">\u6839\u64da\u7db2\u7d61\u985e\u578b\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u7372\u53d6\u7db2\u7d61\u985e\u578b\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u7db2\u7d61\u72c0\u614b\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6839\u64da\u7db2\u7d61\u985e\u578b\u9023\u63a5\u5230\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnector {\n    private static final String WIFI_DB_URL = \"jdbc:mysql:\/\/wifi-db-url:3306\/dbname\";\n    private static final String MOBILE_DB_URL = \"jdbc:mysql:\/\/mobile-db-url:3306\/dbname\";\n    private static final String USER = \"username\";\n    private static final String PASSWORD = \"password\";\n\n    public static Connection connectToDatabase(String networkType) throws SQLException {\n        String dbUrl;\n        if (\"WiFi\".equals(networkType)) {\n            dbUrl = WIFI_DB_URL;\n        } else if (\"Mobile Data\".equals(networkType)) {\n            dbUrl = MOBILE_DB_URL;\n        } else {\n            throw new SQLException(\"No valid network connection\");\n        }\n        return DriverManager.getConnection(dbUrl, USER, PASSWORD);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u6839\u64da\u7db2\u7d61\u985e\u578b\u9078\u64c7\u4e0d\u540c\u7684\u6578\u64da\u5eabURL\u3002\u7576\u7db2\u7d61\u985e\u578b\u70baWiFi\u6642\uff0c\u6211\u5011\u4f7f\u7528WiFi\u7684\u6578\u64da\u5eabURL\uff0c\u7576\u7db2\u7d61\u985e\u578b\u70ba\u79fb\u52d5\u6578\u64da\u6642\uff0c\u6211\u5011\u4f7f\u7528\u79fb\u52d5\u6578\u64da\u7684\u6578\u64da\u5eabURL\u3002\u5982\u679c\u6c92\u6709\u6709\u6548\u7684\u7db2\u7d61\u9023\u63a5\uff0c\u5247\u62cb\u51faSQLException\u3002<\/p>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b-zYXuwBFxna\">\u5be6\u969b\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u4e0a\u8ff0\u4ee3\u78bc\u6574\u5408\u5230\u4e00\u500bActivity\u4e2d\uff0c\u4e26\u5728\u7528\u6236\u555f\u52d5\u61c9\u7528\u6642\u81ea\u52d5\u6aa2\u67e5\u7db2\u7d61\u72c0\u614b\u4e26\u9023\u63a5\u5230\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import android.os.Bundle;\nimport android.widget.Toast;\nimport androidx.appcompat.app.AppCompatActivity;\n\npublic class MainActivity extends AppCompatActivity {\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        String networkType = NetworkUtil.getNetworkType(this);\n        try {\n            Connection connection = DatabaseConnector.connectToDatabase(networkType);\n            Toast.makeText(this, \"Connected to database\", Toast.LENGTH_SHORT).show();\n        } catch (SQLException e) {\n            Toast.makeText(this, \"Database connection failed: \" + e.getMessage(), Toast.LENGTH_LONG).show();\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5728Activity\u7684onCreate\u65b9\u6cd5\u4e2d\u6aa2\u67e5\u7db2\u7d61\u985e\u578b\uff0c\u4e26\u6839\u64da\u7db2\u7d61\u72c0\u614b\u5617\u8a66\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u5982\u679c\u9023\u63a5\u6210\u529f\uff0c\u5247\u986f\u793a\u6210\u529f\u6d88\u606f\uff1b\u5982\u679c\u5931\u6557\uff0c\u5247\u986f\u793a\u932f\u8aa4\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zYXuwBFxna\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u5224\u65b7\u7db2\u7d61\u985e\u578b\u4e26\u6839\u64da\u4e0d\u540c\u7684\u7db2\u7d61\u72c0\u614b\u9023\u63a5\u5230\u6578\u64da\u5eab\u662f\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\u3002\u901a\u904e\u4f7f\u7528ConnectivityManager\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u7372\u53d6\u7576\u524d\u7684\u7db2\u7d61\u72c0\u614b\uff0c\u4e26\u6839\u64da\u9700\u8981\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u61c9\u7528\u7684\u9748\u6d3b\u6027\uff0c\u9084\u80fd\u78ba\u4fdd\u7528\u6236\u5728\u4e0d\u540c\u7684\u7db2\u7d61\u74b0\u5883\u4e0b\u90fd\u80fd\u7372\u5f97\u826f\u597d\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u53ef\u9760\u7684\u5f8c\u7aef\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Android\u4e2d\u5224\u65b7\u7db2\u7d61\u985e\u578b\u4e26\u9023\u63a5\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u61c9\u7528\u5728\u4e0d\u540c\u7db2\u7d61\u74b0\u5883\u4e0b\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-175171","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=175171"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175171\/revisions"}],"predecessor-version":[{"id":175172,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175171\/revisions\/175172"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}