android之檢測手機電池

來源:互聯網
上載者:User

activity代碼:

package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.app.Dialog;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.content.IntentFilter;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.view.Window;<br />import android.view.WindowManager;<br />import android.view.View.OnClickListener;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.Button;<br />import android.widget.TextView;<br />import android.widget.Toast;<br />/***<br /> * @description 查看手機電池電量,這裡要注意監聽Intent.ACTION_BATTERY_CHANGED的廣播接收者,只能<br /> * 在程式中顯示的註冊,不能再androiManifest.xml中進行靜態註冊<br /> * @author chenzheng_java<br /> * @since 2011/03/16<br /> *<br /> */<br />public class BatteryActivity extends Activity implements OnClickListener{</p><p>// 當系統處於充電狀態或者電量發生改變時會廣播該action<br />private static final String ACTION_NAME = Intent.ACTION_BATTERY_CHANGED;</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.battery);</p><p> Button button = (Button) findViewById(R.id.button1);<br /> // 注意哦,我們這裡的activity實現了View.OnClickListener介面<br /> button.setOnClickListener(this);</p><p> }</p><p> // 聲明廣播接收者<br />BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {</p><p>@Override<br />public void onReceive(Context context, Intent intent) {<br />String actionName = intent.getAction();<br />if(actionName.equals(ACTION_NAME)){<br />int level = intent.getIntExtra("level", 0);<br />int scale = intent.getIntExtra("scale", 100);<br />// Toast.makeText(BatteryActivity.this, "level="+level+" scale="+scale, Toast.LENGTH_LONG).show();<br />final Dialog dialog = new Dialog(BatteryActivity.this);<br />dialog.setTitle("電量顯示");</p><p>// 載入xml布局檔案為View對象<br />LayoutInflater flater = LayoutInflater.from(BatteryActivity.this);<br />View view = flater.inflate(R.layout.battery2, null);</p><p>dialog.setContentView(view,new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));</p><p>/**<br /> * 擷取該dialog顯示的父視窗,然後設定當dialog顯示時,視窗內的其他部分設定為模糊<br /> * WindowManager.LayoutParams.FLAG_BLUR_BEHIND告訴我們,無論什麼對象顯示於前段,<br /> * 都會顯示在window的最上層<br /> */<br />Window window = dialog.getWindow();<br />window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);</p><p>/**<br /> * 這裡注意findViewById()方法,預設的會去載入main.xml中的組件。如果你想載入x.xml中的id為<br /> * a的組件,那麼應該在前面加上setContentView這個布局檔案的對象,如這裡的dialog<br /> */<br />TextView textView = (TextView)dialog.findViewById(R.id.textView_message);<br />textView.setText("目前的手機電量為"+level*100/scale+"%");</p><p>Button button2 = (Button) dialog.findViewById(R.id.button_back);<br />button2.setOnClickListener(new OnClickListener() {</p><p>@Override<br />public void onClick(View v) {<br />dialog.dismiss();<br />// 登出廣播接收者<br />unregisterReceiver(broadcastReceiver);</p><p>}<br />});<br />dialog.show();</p><p>}</p><p>}<br />};<br />@Override<br />public void onClick(View v) {<br />IntentFilter filter = new IntentFilter(ACTION_NAME);<br />registerReceiver(broadcastReceiver, filter);<br />}</p><p>}

battery2.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical"><br /><TextView android:text="TextView" android:id="@+id/textView_message"<br />android:textSize="10sp"<br /> android:gravity="center"<br /> android:padding="10px"<br />android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView><br /><Button android:id="@+id/button_back" android:layout_width="wrap_content"<br />android:text="返回" android:layout_height="wrap_content"></Button><br /></LinearLayout><br />

battery.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical"><br /><Button android:text="顯示電量" android:id="@+id/button1"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><br /></LinearLayout><br />

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.