安卓App

新闻类App (MVP + RxJava + Retrofit+

2019-04-24  本文已影响185人  Peakmain

Github地址:新闻类App (MVP + RxJava + Retrofit+Dagger+ARouter)

电量优化介绍和方案

正确认识

方案介绍

         IntentFilter filter=new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        Intent intent=registerReceiver(null,filter);
        LogUtils.e("battery:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1));

缺点:1.价值不大,针对手机整体的耗电量,而非特定App
2.实时性差,精确度低,被动通知

测试

电量辅助监控

运行时能耗

运行时获取使用时长

工具类实现

public class WakeLockUtils {
    private static PowerManager.WakeLock sWakeLock;
    public static void acquire(Context context){
        if(sWakeLock==null){
            sWakeLock=createWakeLock(context);
        }
        if(sWakeLock!=null&&!sWakeLock.isHeld()){
            //唤醒CPU
            sWakeLock.acquire();
        }
    }
    public static void release(){
        if(sWakeLock!=null&&sWakeLock.isHeld()){
            sWakeLock.release();
            sWakeLock=null;
        }
    }
    private static PowerManager.WakeLock createWakeLock(Context context){
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if(pm!=null){
            return pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"");
        }
        return null;
    }
}

获取第一个的时候调用

   WakeLockUtils.acquire(holder.getView(R.id.card_stories).getContext());
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    WakeLockUtils.release();
                                }
                            },200

Lancet

classpath 'me.ele:lancet-plugin:1.0.4'
apply plugin: 'me.ele.lancet'

dependencies {
    provided 'me.ele:lancet-base:1.0.4'
}

hook进行统计时间和线程耗时时间

    @Insert(value = "acquire")
    @TargetClass(value = "com.peakmain.gankzhihu.wakelock.WakeLockUtils", scope = Scope.SELF)
    public static void acquire(Context context) {
        sTrace = Log.getStackTraceString(new Throwable());
        sStartTime = System.currentTimeMillis();
        Origin.callVoid();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                WakeLockUtils.release();
            }
        }, 1000);

    }

    @Insert(value = "release")
    @TargetClass(value = "com.peakmain.gankzhihu.wakelock.WakeLockUtils", scope = Scope.SELF)
    public static void release() {
        Origin.callVoid();
        LogUtils.e("PowerManager " + (System.currentTimeMillis() - sStartTime) + sTrace);
    }
    public  long runTime=0;
   //线程使用时长
    @Insert(value = "run")
    @TargetClass(value = "java.lang.Runnable", scope = Scope.ALL)
    public  void run () {
        runTime=System.currentTimeMillis();
        Origin.callVoid();
        LogUtils.e("runTime " + (System.currentTimeMillis() - runTime));
    }

电量优化套路

CPU

网络相关

定位

界面相关

WakeLock相关

JobScheduler

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class JobSchedulerService extends JobService {
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        //此处执行在主线程
        //模拟一些处理:批量网络请求,APM日志上报
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }
}
 <service android:name=".net.JobSchedulerService"
            android:permission="android.permission.BIND_JOB_SERVICE"/>

使用

    private void startJobScheduler() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
            JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
            //需要连接电源
            builder.setRequiresCharging(true)
                    .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
            jobScheduler.schedule(builder.build());
        }

    }
上一篇 下一篇

猜你喜欢

热点阅读