package com.ritterliu.newBatteryWidget;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class NewBatteryWidget extends AppWidgetProvider{
private static int currentBatteryLevel;
private static int currentBatteryStatus;
private static boolean firstTimeToCreate=true;
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
/** 启动自动更新电池信息的service */
context.startService(new Intent(context,updateService.class));
/** 为AppWidget设置点击事件的响应,启动显示电池信息详情的activity */
Intent startActivityIntent = new Intent(context,NewBatteryInfoActivity.class);
PendingIntent Pintent = PendingIntent.getActivity(context,0,startActivityIntent,0);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.newrelativelayout);
views.setOnClickPendingIntent(R.id.imageView,Pintent);
appWidgetManager.updateAppWidget(appWidgetIds,views);
}
/** 自动更新电池信息的service,通过AlarmManager实现定时不间断地发送电池信息 */
public static class updateService extends Service{
Bitmap bmp; //定义机器人图片
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
/** 定义一个接收电池信息的broascastReceiver */
private BroadcastReceiver batteryReceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
currentBatteryLevel=intent.getIntExtra("level", 0);
currentBatteryStatus=intent.getIntExtra("status", 0);
}
};
private BroadcastReceiver powerConnectedReceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
setViews();
}
};
private BroadcastReceiver powerDisconnectedReceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
setViews();
}
};
/** 设置Widget的显示 */
private void setViews()
{
/** 定义一个AppWidgetManager */
AppWidgetManager manager=AppWidgetManager.getInstance(this);
/** 定义一个RemoteViews,实现对AppWidget界面控制 */
RemoteViews views=new RemoteViews(getPackageName(),R.layout.newrelativelayout);
if(currentBatteryStatus==2||currentBatteryStatus==5) //当正在充电或充满电时,显示充电的图片
{
if(currentBatteryLevel>=95)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.jcharge);
}
else if(currentBatteryLevel>=85&& currentBatteryLevel<95)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.icharge);
}
else if(currentBatteryLevel>=75&& currentBatteryLevel<85)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.hcharge);
}
else if(currentBatteryLevel>=65&& currentBatteryLevel<75)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.gcharge);
}
else if(currentBatteryLevel>=55&& currentBatteryLevel<65)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.fcharge);
}
else if(currentBatteryLevel>=45&& currentBatteryLevel<55)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.echarge);
}
else if(currentBatteryLevel>=35&& currentBatteryLevel<45)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.dcharge);
}
else if(currentBatteryLevel>=25&& currentBatteryLevel<35)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ccharge);
}
else if(currentBatteryLevel>=15&& currentBatteryLevel<25)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.bcharge);
}
else
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.acharge);
}
}
else //未在充电时,显示不在充电状态的系列图片
{
if(currentBatteryLevel>=95)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.j);
}
else if(currentBatteryLevel>=85&& currentBatteryLevel<95)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.i);
}
else if(currentBatteryLevel>=75&& currentBatteryLevel<85)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.h);
}
else if(currentBatteryLevel>=65&& currentBatteryLevel<75)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.g);
}
else if(currentBatteryLevel>=55&& currentBatteryLevel<65)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.f);
}
else if(currentBatteryLevel>=45&& currentBatteryLevel<55)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.e);
}
else if(currentBatteryLevel>=35&& currentBatteryLevel<45)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.d);
}
else if(currentBatteryLevel>=25&& currentBatteryLevel<35)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.c);
}
else if(currentBatteryLevel>=15&& currentBatteryLevel<25)
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.b);
}
else
{
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.a);
}
}
/** 设置AppWidget上显示的图片和文字的内容 */
views.setImageViewBitmap(R.id.imageView,bmp);
views.setTextViewText(R.id.tv,currentBatteryLevel+"%");
ComponentName thisWidget=new ComponentName(this,NewBatteryWidget.class);
/** 更新AppWidget */
manager.updateAppWidget(thisWidget, views);
}
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
/** 注册接收器 */
registerReceiver(batteryReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
/** 增加了对于POWER_CONNECTED和DISCONNECTED事件的监听,
* 以实现充电时信息的动态变化,避免了原来需要依赖AlarmManager
* 每隔一秒发送检测信息来实现,节约了电量,用原来隔一秒更新的方法,
* 经过一夜测试,插件耗电量居然占到了8%,汗。。。
*
* */
registerReceiver(powerConnectedReceiver,new IntentFilter(Intent.ACTION_POWER_CONNECTED));
registerReceiver(powerDisconnectedReceiver,new IntentFilter(Intent.ACTION_POWER_DISCONNECTED ));
/** 使用AlarmManager实现,第一次启动Widget时隔一秒立即更新,
* 以后均为一分钟发送一次更新提示信息,实现信息实时动态变化,
* 实现节电功能
* */
long now=System.currentTimeMillis();
long pause;
if(firstTimeToCreate)
{
firstTimeToCreate=false;
pause=1000;
}
else
{
pause=1000*60*2;
}
Intent alarmIntent=new Intent();
alarmIntent=intent;
PendingIntent pendingIntent=PendingIntent.getService(this, 0, alarmIntent, 0);
AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP,now+pause,pendingIntent);
setViews();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
小程序 电量显示Widgets插件源码.rar
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共84个文件
png:50个
class:17个
xml:5个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 80 浏览量
2023-02-03
11:43:20
上传
评论
收藏 1.7MB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 84 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
大富大贵7
- 粉丝: 322
- 资源: 8868
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)