1

我编写了一个小应用程序,用于在循环中播放单个视频。我怎么能意识到,当连接电源并再次关闭活动时,设备会唤醒并播放此视频。在此之后还应关闭屏幕以避免耗尽电池。

实际代码如下所示:

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;
    }
}

当我在真实设备上测试它时,它每次都说活动仍在运行。但为什么?

谢谢

亚当

4

2 回答 2

3

您可以添加广播接收器以接收电池变化。

获取 Intent.ACTION_BATTERY_CHANGED 的响应

Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

一旦接收器收到检查连接。

正在充电:

安卓USB连接

public static boolean isConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

在清单文件中添加权限:

android.permission.DEVICE_POWER
于 2012-08-31T12:44:59.763 回答
0

我想首先你把它放在清单中。

android.permission.DEVICE_POWER

之后开始广播接收器。然后开始你的活动。希望它会奏效。

于 2012-08-31T12:53:34.103 回答