我编写了一个小应用程序,用于在循环中播放单个视频。我怎么能意识到,当连接电源并再次关闭活动时,设备会唤醒并播放此视频。在此之后还应关闭屏幕以避免耗尽电池。
实际代码如下所示:
package org.adem.receiver;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import org.adem.activities.AdvertiserActivity;
import org.adem.global.GlobalVariables;
public class AppReceiver extends BroadcastReceiver {
private static final String TAG = AppReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
Log.d(TAG, "##Receiver triggered with " + intent.getAction());
if (!checkForRunningActivity(context)) {
initStart(context);
Intent advertiserIntent = new Intent(context, AdvertiserActivity.class);
advertiserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "##Activity started" + advertiserIntent.getComponent().getShortClassName());
context.startActivity(advertiserIntent);
} else {
Log.d(TAG, "##" + intent.getAction() + " received but ignored. Activity still running.");
}
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
Log.d(TAG, "##Receiver triggered with " + intent.getAction());
stopAllAndTurnOff(context);
Log.d(TAG, "##" + intent.getAction() + " received but ignored. Activity is not running.");
}
}
private void initStart(Context context) {
getWakeLock(context).acquire();
GlobalVariables.DEVICE_READY = true;
getKeyGuardLock(context).disableKeyguard();
Log.d(TAG, "##Wake Lock acquired");
}
private void stopAllAndTurnOff(Context context) {
GlobalVariables.DEVICE_READY = false;
if (getWakeLock(context).isHeld()) {
getWakeLock(context).release();
Log.d(TAG, "##Wake Lock released");
}
}
private KeyguardManager.KeyguardLock getKeyGuardLock(Context context) {
if (GlobalVariables.getLock() == null) {
KeyguardManager.KeyguardLock lock;
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE);
GlobalVariables.setLock(lock);
Log.d(TAG, "##New KeyGuard initialized");
}
return GlobalVariables.getLock();
}
private PowerManager.WakeLock getWakeLock(Context context) {
if (GlobalVariables.getWl() == null) {
PowerManager.WakeLock wl;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
GlobalVariables.setWl(wl);
Log.d(TAG, "##New Wake Lock initialized");
}
return GlobalVariables.getWl();
}
private boolean checkForRunningActivity(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
for (ActivityManager.RunningTaskInfo task : manager.getRunningTasks(Integer.MAX_VALUE)) {
Log.d(TAG, task.baseActivity.getClassName());
if (task.baseActivity.getClassName().equals("org.adem.activities.AdvertiserActivity")
|| task.baseActivity.getClassName().equals("org.adem.activities.VideoViewActivity")) {
return true;
}
}
return false;
}
}
当我在真实设备上测试它时,它每次都说活动仍在运行。但为什么?
谢谢
亚当