gpt4 book ai didi

android电源信息查看(电量、温度、电压)实例代码

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 29 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章android电源信息查看(电量、温度、电压)实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了android电源信息查看方法。分享给大家供大家参考。具体如下:

1. PowerTestActivity:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
/**
  * So you thought sync used up your battery life.
  */
public class PowerTestActivity extends Activity {
   TextView mLog;
   DateFormat mDateFormat;
   IntentFilter mFilter;
   PowerManager.WakeLock mWakeLock;
   SpinThread mThread;
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     // Set the layout for this activity. You can find it
     // in res/layout/hello_activity.xml
     setContentView(R.layout.main);
     findViewById(R.id.checkbox).setOnClickListener(mClickListener);
     mLog = (TextView)findViewById(R.id.log);
     mDateFormat = DateFormat.getInstance();
     mFilter = new IntentFilter();
     mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
     mFilter.addAction(Intent.ACTION_BATTERY_LOW);
     mFilter.addAction(Intent.ACTION_BATTERY_OKAY);
     mFilter.addAction(Intent.ACTION_POWER_CONNECTED);
     PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
     mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster" );
     mWakeLock.setReferenceCounted( false );
   }
   @Override
   public void onPause() {
     stopRunning();
   }
   View.OnClickListener mClickListener = new View.OnClickListener() {
     public void onClick(View v) {
       CheckBox checkbox = (CheckBox)v;
       if (checkbox.isChecked()) {
         startRunning();
       } else {
         stopRunning();
       }
     }
   };
   void startRunning() {
     log( "Start" );
     registerReceiver(mReceiver, mFilter);
     mWakeLock.acquire();
     if (mThread == null ) {
       mThread = new SpinThread();
       mThread.start();
     }
   }
   void stopRunning() {
     log( "Stop" );
     unregisterReceiver(mReceiver);
     mWakeLock.release();
     if (mThread != null ) {
       mThread.quit();
       mThread = null ;
     }
   }
   void log(String s) {
     mLog.setText(mLog.getText() + "\n" + mDateFormat.format( new Date()) + ": " + s);
   }
   BroadcastReceiver mReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
       StringBuffer sb = new StringBuffer();
       String action = intent.getAction();
       /*
        * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()
        */
       if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
         sb.append( "\n当前电量:" ).append(intent.getIntExtra( "level" , 0 ));
         sb.append( "\n电池电量:" ).append(intent.getIntExtra( "scale" , 100 ));
         // 电池伏数
         sb.append( "\n当前电压:" ).append(intent.getIntExtra( "voltage" , 0 ));
         // 电池温度
         sb.append( "\n当前温度:" ).append(
             intent.getIntExtra( "temperature" , 0 ));
         String BatteryStatus = null ;
         switch (intent.getIntExtra( "status" ,
             BatteryManager.BATTERY_STATUS_UNKNOWN)) {
         case BatteryManager.BATTERY_STATUS_CHARGING:
           BatteryStatus = "充电状态" ;
           break ;
         case BatteryManager.BATTERY_STATUS_DISCHARGING:
           BatteryStatus = "放电状态" ;
           break ;
         case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
           BatteryStatus = "未充电" ;
           break ;
         case BatteryManager.BATTERY_STATUS_FULL:
           BatteryStatus = "充满电" ;
           break ;
         case BatteryManager.BATTERY_STATUS_UNKNOWN:
           BatteryStatus = "未知道状态" ;
           break ;
         }
         sb.append( "\n当前状态:" ).append(BatteryStatus);
         String BatteryStatus2 = null ;
         switch (intent.getIntExtra( "plugged" ,
             BatteryManager.BATTERY_PLUGGED_AC)) {
         case BatteryManager.BATTERY_PLUGGED_AC:
           BatteryStatus2 = "AC充电" ;
           break ;
         case BatteryManager.BATTERY_PLUGGED_USB:
           BatteryStatus2 = "USB充电" ;
           break ;
         }
         sb.append( "\n充电方式:" ).append(BatteryStatus2);
         String BatteryTemp = null ;
         switch (intent.getIntExtra( "health" ,
             BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
         case BatteryManager.BATTERY_HEALTH_UNKNOWN:
           BatteryTemp = "未知错误" ;
           break ;
         case BatteryManager.BATTERY_HEALTH_GOOD:
           BatteryTemp = "状态良好" ;
           break ;
         case BatteryManager.BATTERY_HEALTH_DEAD:
           BatteryTemp = "电池没有电" ;
           break ;
         case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
           BatteryTemp = "电池电压过高" ;
           break ;
         case BatteryManager.BATTERY_HEALTH_OVERHEAT:
           BatteryTemp = "电池过热" ;
           break ;
         }
         sb.append( "\n电池状态:" ).append(BatteryTemp);
         log(sb.toString());
       }
     }
   };
   class SpinThread extends Thread {
     private boolean mStop;
     public void quit() {
       synchronized ( this ) {
         mStop = true ;
       }
     }
     public void run() {
       while ( true ) {
         synchronized ( this ) {
           if (mStop) {
             return ;
           }
         }
       }
     }
   }
}

2. main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "fill_parent"
   android:layout_height = "fill_parent"
   >
< CheckBox android:id = "@+id/checkbox"
     android:layout_width = "fill_parent"
     android:layout_height = "wrap_content"
     android:layout_marginLeft = "25dp"
     android:layout_marginTop = "25dp"
     android:textSize = "18sp"
     android:textColor = "#ffffffff"
     android:text = "电源测试"
     />
   < ScrollView android:id = "@+id/scroll"
     android:layout_width = "fill_parent"
     android:layout_height = "0px"
     android:layout_weight = "1"
     >
     < TextView android:id = "@+id/log"
       android:layout_width = "fill_parent"
       android:layout_height = "wrap_content"
       android:layout_marginTop = "25dp"
       android:textSize = "12sp"
       android:textColor = "#ffffffff"
       />
   </ ScrollView >
</ LinearLayout >

3. AndroidManifest.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
    package = "com.lenovo.android"
    android:versionCode = "1"
    android:versionName = "1.0" >
   < uses-sdk android:minSdkVersion = "8" />
   < application android:icon = "@drawable/icon" android:label = "@string/app_name" >
     < activity android:name = ".PowerTestActivity"
          android:label = "@string/app_name" >
       < intent-filter >
         < action android:name = "android.intent.action.MAIN" />
         < category android:name = "android.intent.category.LAUNCHER" />
       </ intent-filter >
     </ activity >
   </ application >
   < uses-permission android:name = "android.permission.WAKE_LOCK" />
   < uses-permission android:name = "android.permission.DEVICE_POWER" />
</ manifest >

希望本文所述对大家的Android程序设计有所帮助.

最后此篇关于android电源信息查看(电量、温度、电压)实例代码的文章就讲到这里了,如果你想了解更多关于android电源信息查看(电量、温度、电压)实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com