简体   繁体   English

Android 自定义类 - 钩入 Activity 生命周期

[英]Android custom class - Hook into Activity Lifecycle

I have an activity in which I want to monitor battery life.我有一项活动,我想在其中监控电池寿命。 I want to decouple the battery life into it's own class.我想将电池寿命分离到它自己的类中。 I could do the following in the activity我可以在活动中执行以下操作

onPause() {
  batteryChecker = new BatteryChecker();
}
onResume() {
   batteryChecker.kill();
}

I would prefer to just do this:我宁愿这样做:

onCreate() {
  bg = new BatteryChecker(this);
}

Is there any way I can get BatteryChecker to respond to the onPause/onResume of the attached activity?有什么办法可以让 BatteryChecker 响应附加活动的 onPause/onResume?

Your BatteryChecker can implement ActivityLifecycleCallbacks and call registerActivityLifecycleCallbacks in onCreate when you create your Application class, in this case your custom class will receive all Activity lifecycle callbacks.当您创建Application类时,您的BatteryChecker可以实现ActivityLifecycleCallbacks并在onCreate调用registerActivityLifecycleCallbacks ,在这种情况下,您的自定义类将接收所有 Activity 生命周期回调。

see more here - ActivityLifecycleCallbacks在这里查看更多 - ActivityLifecycleCallbacks

It's of arguable benefit because you still have to use Activity events, but you could attach to the Activity lifecycle in your activity's onCreate() and detach from it in its onDestroy() events too.这是有争议的好处,因为您仍然必须使用Activity事件,但是您可以在ActivityonCreate()附加到Activity生命周期,并在其onDestroy()事件中与它分离。

This may help from a hygiene perspective depending on how "busy" your code is in its existing lifecycle blocks, but doesn't necessarily make it a good practice.从卫生的角度来看,这可能会有所帮助,具体取决于您的代码在其现有生命周期块中的“繁忙”程度,但不一定使它成为一种好的做法。

In your BatteryChecker class:在您的BatteryChecker类中:

public class BatteryChecker implements Application.ActivityLifecycleCallbacks {
    private boolean paused = true ;
...
    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        if (paused) doResumeStuff();
        paused = false;
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        if (!paused) doPauseStuff();
        paused = true;
    }
}

And in your activity:在您的活动中:

private BatteryChecker batteryChecker = null;

@Override
public void onCreate(final Bundle savedInstanceState) {
   batteryChecker = new BatteryChecker();
   getApplication().registerActivityLifecycleCallbacks(batteryChecker);
}

@Override
public void onDestroy() {
   getApplication().unRegisterActivityLifecycleCallbacks(batteryChecker);
}

It should also be noted you'll never see the onActivityDestroyed() event fire in your BatteryChecker class as its scope is within that of the actual activity's lifecycle.还应该注意的是,您永远不会在BatteryChecker类中看到onActivityDestroyed()事件触发,因为它的范围在实际活动的生命周期内。

You need a class extends broadcastReceiver你需要一个类扩展广播接收器

 public class PowerConnectionReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                                status == BatteryManager.BATTERY_STATUS_FULL;

            int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
            boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
        }
    }

more info.更多信息。 Monitoring Battery State 监控电池状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM