/*******************************************************************************
* Copyright (c) 2009 Ferenc Hechler - ferenc_hechler@users.sourceforge.net
*
* This file is part of the Android Battery Dog
*
* The Android Battery Dog is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later version.
*
* The Android Battery Dog is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Android Battery Dog;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*******************************************************************************/
package com.test.mybatteryapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class MyBatteryActivity extends Activity {
private final static String TAG = "MyBatteryActivity";
private final static int MENU_4H = 1;
private final static int MENU_8H = 2;
private final static int MENU_24H = 3;
private final static int MENU_3DAYS = 4;
private final static int MENU_7DAYS = 5;
private final static int MENU_ALL = 6;
private final static int margXLeft = 5;
private final static int margXRight = 5;
private final static int margYTop = 60;
private final static int margYBottom = 5;
private long width = 300;
private long height = 300;
private long w = width - margXLeft - margXRight;
private long h = height - margYTop - margYBottom;
private long msecPerHour = 1000*60*60;
private long mDeltaTime = 24*msecPerHour;
private long mOffset = 0;
private GraphView mGraphView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGraphView = new GraphView(this);
setContentView(mGraphView);
}
/**
* Called when your activity's options menu needs to be created.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_4H, Menu.NONE, "4h");
menu.add(Menu.NONE, MENU_8H, Menu.NONE, "8h");
menu.add(Menu.NONE, MENU_24H, Menu.NONE, "24h");
menu.add(Menu.NONE, MENU_3DAYS, Menu.NONE, "3 days");
menu.add(Menu.NONE, MENU_7DAYS, Menu.NONE, "7 days");
menu.add(Menu.NONE, MENU_ALL, Menu.NONE, "all");
return true;
}
/**
* Called when a menu item is selected.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == MENU_4H) {
mDeltaTime = 4*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_8H) {
mDeltaTime = 8*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_24H) {
mDeltaTime = 24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_3DAYS) {
mDeltaTime = 3*24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_7DAYS) {
mDeltaTime = 7*24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_ALL) {
mDeltaTime = 0;
mOffset = 0;
mGraphView.invalidate();
}
mGraphView.readRecords(); // update from service
return true;
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
super.onTrackballEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mOffset = 0;
mGraphView.readRecords(); // update from service
mGraphView.invalidate();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float x = event.getRawX();
if (x < 0) {
mOffset -= mDeltaTime/5;
mGraphView.invalidate();
}
else if (x>0) {
mOffset += mDeltaTime/5;
if (mOffset > 0)
mOffset = 0;
mGraphView.invalidate();
}
}
return true;
}
private class GraphView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private BatteryRecord[] mRecords;
private float mLastX;
public void readRecords() {
try {
mRecords = readLog();
}
catch (Exception e) {
Log.e(TAG,e.getMessage(), e);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mLastX = event.getRawX();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
// Log.i(TAG, event.toString()+" - " + event.getHistorySize());
float x = event.getRawX();
float dx = x-mLastX;
mLastX = x;
long ldx = (long)(mDeltaTime*dx/width);
mOffset -= ldx;
if (mOffset > 0)
mOffset = 0;
mGraphView.invalidate();
}
return true;
}
public GraphView(Context context) {
super(context);
readRecords();
Display display = ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
w = width - margXLeft - margXRight;
h = height - margYTop - margYBottom;
}
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
paint.setStrokeWidth(1);
Paint paintP = new Paint(Paint.ANTI_ALIAS_FLAG);
paintP.setStrokeWidth(2);
paintP.setColor(Color.YELLOW);
Paint paintV = new Paint();
paintV.setStrokeWidth(0);
paintV.setColor(Color.RED);
Paint paintT = new Paint();
paintT.setStrokeWidth(0);
paintT.setColor(Color.GREEN);
Paint paintD = new Paint(); // discharge rate
paintD.setStrokeWidth(0);
paintD.setColor(Color.CYAN);
canvas.drawColor(Color.BLACK);
if ((mRecords == null) || (mRecords.length == 0)) {
paint.setColor(Color.WHITE);
canvas.drawText("no data found", 10, 50, paint);
return;
}
drawMarker(canvas, paintP, paintV, paintT, paintD);
int maxRec = mRecords.length;
long minTime = mRecords[0].timestamp;
long maxTime = mRecords[maxRec-1].timestamp;
long dTime = maxTime-minTime;
if (mDeltaTime != 0) {
// make minTime, maxTime be min and max time on screen
dTime = mDeltaTime;
minTime = maxTime-dTime+mOffset;
maxTime = minTime+mDeltaTime;
}
BatteryRecord rec;
BatteryRecord oldRec;
for (int i = 0; i <= maxRec; i++) {
if (i == 0)
oldRec = mRecords[0];
else
oldRec = mRecords[i-1];
if (i == maxRec)
rec = mRecords[maxRec-1];
没有合适的资源?快使用搜索试试~ 我知道了~
Android-电池管理-实时获取电池电量,温度...等属性。
共39个文件
xml:12个
png:10个
java:6个
5星 · 超过95%的资源 需积分: 5 8 下载量 163 浏览量
2022-04-20
19:52:45
上传
评论 1
收藏 150KB ZIP 举报
温馨提示
实现一个电池属性实时监听器。这些属性包括温度,电池电量....等等。而且,使用Service,Broadcast,以及Thread的方式来实现。MyBatteryService后台获取Battery属性值,然后,在Activity中进行相关数据的显示。
资源详情
资源评论
资源推荐
收起资源包目录
MyBatteryApp.zip (39个子文件)
MyBatteryApp
gradlew.bat 2KB
gradlew 5KB
gradle.properties 1003B
app
proguard-rules.pro 750B
src
test
java
com
test
mybatteryapp
ExampleUnitTest.java 382B
androidTest
java
com
test
mybatteryapp
ExampleInstrumentedTest.java 756B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 16KB
drawable
ic_launcher_background.xml 5KB
mipmap-hdpi
ic_launcher.png 4KB
ic_launcher_round.png 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 378B
themes.xml 834B
strings.xml 74B
values-night
themes.xml 834B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 12KB
mipmap-mdpi
ic_launcher.png 3KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher_round.xml 272B
ic_launcher.xml 272B
layout
fragment_bottom.xml 1KB
activity_main.xml 2KB
fragment_top.xml 1KB
java
com
test
mybatteryapp
FileUtils.java 184B
MyBatteryService.java 6KB
MainActivity.java 7KB
MyBatteryActivity.java 16KB
AndroidManifest.xml 860B
.gitignore 6B
build.gradle 1000B
libs
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 232B
.gitignore 225B
settings.gradle 49B
build.gradle 613B
共 39 条
- 1
liranke
- 粉丝: 4797
- 资源: 72
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5