首页 > 其他分享 >android获取手机电量

android获取手机电量

时间:2023-04-07 10:09:08浏览次数:42  
标签:Button intScale 获取 电量 import android intLevel public


import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class BatteryInfo extends Activity
{
  /* 变量声明 */
  private int intLevel;
  private int intScale;
  private Button mButton01;

  /* 创建BroadcastReceiver */
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()
  {
    public void onReceive(Context context, Intent intent)
    {
      String action = intent.getAction();
      /*
       * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()
       */
      if (Intent.ACTION_BATTERY_CHANGED.equals(action))
      {
        intLevel = intent.getIntExtra("level", 0);
        intScale = intent.getIntExtra("scale", 100);
        Log.i("", "intLevel = " + intLevel);
        Log.i("", "intScale = " + intScale);
        onBatteryInfoReceiver(intLevel, intScale);
      }
    }
  };

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton01 = (Button) findViewById(R.id.myButton1);
    mButton01.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {

        // 注册一个系统 BroadcastReceiver,作为访问电池计量之用
        // 這個不能直接在AndroidManifest.xml中註冊
        registerReceiver(mBatInfoReceiver, new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED));
      }
    });
  }

  /* 捕捉到ACTION_BATTERY_CHANGED时要运行的method */
  public void onBatteryInfoReceiver(int intLevel, int intScale)
  {
    /* create 跳出的对话窗口 */
    final Dialog d = new Dialog(BatteryInfo.this);
    d.setTitle(R.string.str_dialog_title);
    d.setContentView(R.layout.mydialog);

    /* 创建一个背景模糊的Window,且将对话窗口放在前景 */
    Window window = d.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

    /* 将取得的电池计量显示于Dialog中 */
    TextView mTextView02 = (TextView) d.findViewById(R.id.myTextView2);
    mTextView02.setText(getResources()
        .getText(R.string.str_dialog_body)
        + String.valueOf(intLevel * 100 / intScale) + "%");

    /* 设置返回主画面的按钮 */
    Button mButton02 = (Button) d.findViewById(R.id.myButton2);
    mButton02.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
        /* 反注册Receiver,并关闭对话窗口 */
        unregisterReceiver(mBatInfoReceiver);
        d.dismiss();
      }
    });
    d.show();
  }
}



代码里还用到了自定义的Dialog布局:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:background="@drawable/white" android:orientation="vertical"
	android:layout_width="fill_parent" android:layout_height="fill_parent">
	<TextView android:id="@+id/myTextView2" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:textColor="@drawable/blue"
		android:textSize="16sp" android:gravity="center" android:padding="10px">
	</TextView>
	<Button android:id="@+id/myButton2" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/str_button2">
	</Button>
</LinearLayout>




http://edison-cool911.iteye.com/blog/737781


标签:Button,intScale,获取,电量,import,android,intLevel,public
From: https://blog.51cto.com/u_5454003/6174575

相关文章

  • android自定义Activity窗口大小(theme运用)
    正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示:实现起来非常简单。第一步,创建一个背景配置文件float_box.xml,放到res/drawable下,如下所示(如看不懂可查看本站:):<?xmlversion="1.0"encoding="utf-8"......
  • 自己早期(android1.1)做的一个绘图练习
    仅仅是个练习而已!只做了第一个菜单的事件。其他都没事件。代码片段publicmPayCanvas(Contextcontext){ super(context); //获得屏幕宽高 WindowManagerwindowManager=getWindowManager(); Displaydisplay=windowManager.g......
  • android 拖拽图片&拖动浮动按钮到处跑
    来自老外:importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.os.Bundle;importandroid.view.MotionEvent;importandroid.widget.AbsoluteLayout;importandroid.widget.Button;public......
  • android打造的最简单计算器界面
    先看图:这里主要是锻炼一下TableLayout布局,注意其中的android:stretchColumns="0,1,2,3"属性,该属性可以控制每列的宽的权重,类似weight,由于这里4列都是“平等的”,所以是“0,1,2,3”,全部布局文件如下:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:an......
  • android之简单数据存储Preference
    这里的持久化其实就是本地配置文件的读写,实现方法是通过Activity.getPreferences(int)获取SharedPreferences对象,然后操作配置文件的读写,值得注意的是以下几点:1)Activity.getPreferences(intmode)等价于Content.getSharedPreferences(Stringfilename,intmod......
  • Android存储用户登录信息最好的方式之一-Shared Preferences
    对于Android应用程序,存储用户登录信息的最佳方式是使用SharedPreferences。SharedPreferences是Android提供的一个轻量级存储机制,可以存储简单的键值对数据。它非常适合存储用户设置、用户偏好和其他应用程序数据,包括登录信息。SharedPreferences具有以下优点:轻量级:Shared......
  • udp协议的获取时间的客户端
    #include<stdio.h>#include<stdlib.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<errno.h>#include<string.h>constintmaxline=4096;voiddg_cli(FILE*fp,intsockfd,stru......
  • 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
    DeviceYearClass的主要功能是根据CPU核数、时钟频率以及内存大小对设备进行分级。下表是Facebook公司提供的分级标准,其中Year栏表示分级结果。以下类就是从Facebook的开源项目DeviceYearClass中拿出来的packagecom.yirui.youbao.util;importandroid.annotati......
  • 今日总结 Android与硬件设备通信(一)
    今天看了看Android与硬件通信的原理1、建立连接的原理在实现软件与硬件交互的时候,首先需要了解该硬件的构造,运行流程等相关操作,熟读相关的文档,知道硬件开发商那边给出了哪些接口,分别对应我们项目中的那些模块等等就拿我这个项目的硬件来说,供应商给出的连接流程如下图:  我......
  • Android中asset文件夹和raw文件夹区别
    res/raw和assets的相同点:1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。res/raw和assets的不同点:1.res/raw中的文件会被映射到R.java文件中,访问的时候直接使用资源ID即R.id.filename;assets文件夹下的文件不会被映射到R.java......