Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:15:32+00:00 2026-06-10T14:15:32+00:00

I wrote a small application for playing a single video in the loop. How

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:15:34+00:00Added an answer on June 10, 2026 at 2:15 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface . I wrote a small application in C that plays a video file
(Using WPF) In a small application that I wrote, I am using some count-down
I wrote a very small application, which access the Remote Power Shell of Exchanger
I'm a newbie iOS developer. I wrote a small application that save an NSMutableArray
I have a small application that I wrote that imports both the iTunes and
I'm looking to improve this query I wrote for a small web application in
I wrote a small application to use as a sandbox for testing ideas that
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I wrote a small PHP application that I'd like to distribute. I'm looking for
I wrote a very small web application that sends your GPS coordinates to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.