Reputation: 406
I wrote a small application for playing a single video in the loop. How can i realize, that the device wakes up and plays this video when power is connected and closes the activity again . After this also the screen should be turned off to avoid draining the battery.
The actual code looks like:
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;
}
}
When I test this on a real device, it say every time, that the activity is still running. But why?
Thanks
Adem
Upvotes: 1
Views: 968
Reputation: 2499
You can add broadcast receiver to receive the battery changes.
Get responds for Intent.ACTION_BATTERY_CHANGED
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
once receiver received check the connection.
Plugged In/Charging:
android USB connection
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;
}
Add the permission in manifest file :
android.permission.DEVICE_POWER
Upvotes: 3
Reputation: 1287
I think first you put this in manifest.
android.permission.DEVICE_POWER
After that start broadcast receiver.then start your activity. Hope it will work .
Upvotes: 0