﻿{"id":1788,"date":"2012-02-12T17:14:05","date_gmt":"2012-02-12T09:14:05","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1788"},"modified":"2012-02-12T17:14:05","modified_gmt":"2012-02-12T09:14:05","slug":"android%e8%8e%b7%e5%8f%96%e6%89%8b%e6%9c%ba%e7%94%b5%e9%87%8f","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1788","title":{"rendered":"android\u83b7\u53d6\u624b\u673a\u7535\u91cf"},"content":{"rendered":"<pre class=\"brush: java\">\r\nimport android.app.Activity;\r\nimport android.app.Dialog;\r\nimport android.content.BroadcastReceiver;\r\nimport android.content.Context;\r\nimport android.content.Intent;\r\nimport android.content.IntentFilter;\r\nimport android.os.Bundle;\r\nimport android.util.Log;\r\nimport android.view.View;\r\nimport android.view.Window;\r\nimport android.view.WindowManager;\r\nimport android.widget.Button;\r\nimport android.widget.TextView;\r\n\r\npublic class BatteryInfo extends Activity\r\n{\r\n  \/* \u53d8\u91cf\u58f0\u660e *\/\r\n  private int intLevel;\r\n  private int intScale;\r\n  private Button mButton01;\r\n\r\n  \/* \u521b\u5efaBroadcastReceiver *\/\r\n  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()\r\n  {\r\n    public void onReceive(Context context, Intent intent)\r\n    {\r\n      String action = intent.getAction();\r\n      \/*\r\n       * \u5982\u679c\u6355\u6349\u5230\u7684action\u662fACTION_BATTERY_CHANGED\uff0c \u5c31\u8fd0\u884conBatteryInfoReceiver()\r\n       *\/\r\n      if (Intent.ACTION_BATTERY_CHANGED.equals(action))\r\n      {\r\n        intLevel = intent.getIntExtra(&quot;level&quot;, 0);\r\n        intScale = intent.getIntExtra(&quot;scale&quot;, 100);\r\n        Log.i(&quot;&quot;, &quot;intLevel = &quot; + intLevel);\r\n        Log.i(&quot;&quot;, &quot;intScale = &quot; + intScale);\r\n        onBatteryInfoReceiver(intLevel, intScale);\r\n      }\r\n    }\r\n  };\r\n\r\n  \/** Called when the activity is first created. *\/\r\n  @Override\r\n  public void onCreate(Bundle savedInstanceState)\r\n  {\r\n    super.onCreate(savedInstanceState);\r\n    setContentView(R.layout.main);\r\n\r\n    mButton01 = (Button) findViewById(R.id.myButton1);\r\n    mButton01.setOnClickListener(new Button.OnClickListener()\r\n    {\r\n      public void onClick(View v)\r\n      {\r\n\r\n        \/\/ \u6ce8\u518c\u4e00\u4e2a\u7cfb\u7edf BroadcastReceiver\uff0c\u4f5c\u4e3a\u8bbf\u95ee\u7535\u6c60\u8ba1\u91cf\u4e4b\u7528\r\n        \/\/ \u9019\u500b\u4e0d\u80fd\u76f4\u63a5\u5728AndroidManifest.xml\u4e2d\u8a3b\u518a\r\n        registerReceiver(mBatInfoReceiver, new IntentFilter(\r\n            Intent.ACTION_BATTERY_CHANGED));\r\n      }\r\n    });\r\n  }\r\n\r\n  \/* \u6355\u6349\u5230ACTION_BATTERY_CHANGED\u65f6\u8981\u8fd0\u884c\u7684method *\/\r\n  public void onBatteryInfoReceiver(int intLevel, int intScale)\r\n  {\r\n    \/* create \u8df3\u51fa\u7684\u5bf9\u8bdd\u7a97\u53e3 *\/\r\n    final Dialog d = new Dialog(BatteryInfo.this);\r\n    d.setTitle(R.string.str_dialog_title);\r\n    d.setContentView(R.layout.mydialog);\r\n\r\n    \/* \u521b\u5efa\u4e00\u4e2a\u80cc\u666f\u6a21\u7cca\u7684Window\uff0c\u4e14\u5c06\u5bf9\u8bdd\u7a97\u53e3\u653e\u5728\u524d\u666f *\/\r\n    Window window = d.getWindow();\r\n    window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,\r\n        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);\r\n\r\n    \/* \u5c06\u53d6\u5f97\u7684\u7535\u6c60\u8ba1\u91cf\u663e\u793a\u4e8eDialog\u4e2d *\/\r\n    TextView mTextView02 = (TextView) d.findViewById(R.id.myTextView2);\r\n    mTextView02.setText(getResources()\r\n        .getText(R.string.str_dialog_body)\r\n        + String.valueOf(intLevel * 100 \/ intScale) + &quot;%&quot;);\r\n\r\n    \/* \u8bbe\u7f6e\u8fd4\u56de\u4e3b\u753b\u9762\u7684\u6309\u94ae *\/\r\n    Button mButton02 = (Button) d.findViewById(R.id.myButton2);\r\n    mButton02.setOnClickListener(new Button.OnClickListener()\r\n    {\r\n      public void onClick(View v)\r\n      {\r\n        \/* \u53cd\u6ce8\u518cReceiver\uff0c\u5e76\u5173\u95ed\u5bf9\u8bdd\u7a97\u53e3 *\/\r\n        unregisterReceiver(mBatInfoReceiver);\r\n        d.dismiss();\r\n      }\r\n    });\r\n    d.show();\r\n  }\r\n}\r\n<\/pre>\n<p>\u4ee3\u7801\u91cc\u8fd8\u7528\u5230\u4e86\u81ea\u5b9a\u4e49\u7684Dialog\u5e03\u5c40: <\/p>\n<pre class=\"brush: xml\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n\tandroid:background=&quot;@drawable\/white&quot; android:orientation=&quot;vertical&quot;\r\n\tandroid:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt;\r\n\t&lt;TextView android:id=&quot;@+id\/myTextView2&quot; android:layout_width=&quot;fill_parent&quot;\r\n\t\tandroid:layout_height=&quot;wrap_content&quot; android:textColor=&quot;@drawable\/blue&quot;\r\n\t\tandroid:textSize=&quot;16sp&quot; android:gravity=&quot;center&quot; android:padding=&quot;10px&quot;&gt;\r\n\t&lt;\/TextView&gt;\r\n\t&lt;Button android:id=&quot;@+id\/myButton2&quot; android:layout_width=&quot;wrap_content&quot;\r\n\t\tandroid:layout_height=&quot;wrap_content&quot; android:text=&quot;@string\/str_button2&quot;&gt;\r\n\t&lt;\/Button&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import android.app.Activity; import android.app.Dialog; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[346],"tags":[512],"class_list":["post-1788","post","type-post","status-publish","format-standard","hentry","category-android","tag-512"],"views":770,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1788"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1788\/revisions"}],"predecessor-version":[{"id":1789,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1788\/revisions\/1789"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}