Example usage for android.content Intent ACTION_SCREEN_OFF

List of usage examples for android.content Intent ACTION_SCREEN_OFF

Introduction

In this page you can find the example usage for android.content Intent ACTION_SCREEN_OFF.

Prototype

String ACTION_SCREEN_OFF

To view the source code for android.content Intent ACTION_SCREEN_OFF.

Click Source Link

Document

Broadcast Action: Sent when the device goes to sleep and becomes non-interactive.

Usage

From source file:com.apexlabs.alarm.AlarmService.java

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    editor = prefs.edit();/*ww  w .  ja v a2s. c  o m*/

    filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);

    listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            Log.d(TAG, "Shared Preferences Change Listener");
            if (key.equals(screenOnKey)) {
                boolean b = prefs.getBoolean(screenOnKey, false);
                if (b) {
                    Log.d(TAG, "Screen is On!");
                    handler.removeCallbacks(r);
                    handler.postDelayed(r, 1);
                } else {
                    Log.d(TAG, "Screen is Off!");
                    clearNotification(867);
                    handler.removeCallbacks(r);
                }
            }
        }
    };

    prefs.registerOnSharedPreferenceChangeListener(listener);
}

From source file:com.golden.android.eyecare.ForegroundService.java

@Override
public void onCreate() {
    super.onCreate();

    context = getApplicationContext();/*  w w  w . jav a  2s.c  om*/
    global = (Global) getApplicationContext();

    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    final BroadcastReceiver screenoffReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                Log.v("screenoffReceiver", "SCREEN OFF");
                onDestroy();
                stopForeground(Boolean.TRUE);
            }
            return;
        }
    };
    registerReceiver(screenoffReceiver, filter);

}

From source file:step.StepService.java

private void init() {

    // Start detecting
    mStepDetector = StepDetector.getIns();
    mStepDetector2 = StepCounterDetector.getIns();
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    registerDetector();//from  www .j av a  2s . c  o  m

    // Register our receiver for the ACTION_SCREEN_OFF action. This will make our receiver
    // code be called whenever the phone enters standby mode.
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(mReceiver, filter);

    acquireWakeLock();

    new Thread() {
        @Override
        public void run() {
            StepDBManager.getIns().getStepCountFromDB();
        }
    }.start();

    singleThreadExecutor.execute(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    StepDBManager.getIns().writeStepToDb();
                    Thread.sleep(timenterval * 1000);
                    //                        Thread.sleep(1000);
                    //                        ?
                    //                                                Vibrator vibrator = (Vibrator)
                    // ServicePlatformApp.getAppContext()
                    //                         .getSystemService(ServicePlatformApp.getAppContext()
                    // .VIBRATOR_SERVICE);
                    //                                                vibrator.vibrate(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

}

From source file:sintef.android.gravity.AlarmService.java

@Override
public void onCreate() {
    EventBus.getDefault().registerSticky(this);

    Controller.initializeController(this);
    SoundHelper.initializeSoundsHelper(this);
    PreferencesHelper.initializePreferences(this);

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, AlarmService.class.getName());

    registerReceiver(mScreenStateReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    EventBus.getDefault().post(EventTypes.ONRESUME);
}

From source file:air.com.snagfilms.cast.chromecast.notifications.VideoCastNotificationService.java

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate()");
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mBroadcastReceiver = new BroadcastReceiver() {

        @Override/*from   ww w . j av  a 2  s. c  o m*/
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "onReceive(): " + intent.getAction());
        }
    };

    registerReceiver(mBroadcastReceiver, filter);

    readPersistedData();
    mCastManager = VideoChromeCastManager.initialize(this, mApplicationId, mTargetActivity, null);
    if (!mCastManager.isConnected()) {
        mCastManager.reconnectSessionIfPossible(this, false);
    }
    mConsumer = new VideoCastConsumerImpl() {
        @Override
        public void onApplicationDisconnected(int errorCode) {
            Log.d(TAG, "onApplicationDisconnected() was reached");
            stopSelf();
        }

        @Override
        public void onRemoteMediaPlayerStatusUpdated() {
            int mediaStatus = mCastManager.getPlaybackStatus();
            VideoCastNotificationService.this.onRemoteMediaPlayerStatusUpdated(mediaStatus);
        }

    };
    mCastManager.addVideoCastConsumer(mConsumer);
}

From source file:com.firefly.sample.castcompanionlibrary.notification.VideoCastNotificationService.java

@Override
public void onCreate() {
    super.onCreate();
    LOGD(TAG, "onCreate()");
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mBroadcastReceiver = new BroadcastReceiver() {

        @Override/*w w  w .  j av  a 2s .  c  o m*/
        public void onReceive(Context context, Intent intent) {
            LOGD(TAG, "onReceive(): " + intent.getAction());
        }
    };

    registerReceiver(mBroadcastReceiver, filter);

    readPersistedData();
    mCastManager = VideoCastManager.initialize(this, mApplicationId, mTargetActivity, mDataNamespace);
    if (!mCastManager.isConnected()) {
        mCastManager.reconnectSessionIfPossible(this, false);
    }
    mConsumer = new VideoCastConsumerImpl() {
        @Override
        public void onApplicationDisconnected(int errorCode) {
            LOGD(TAG, "onApplicationDisconnected() was reached");
            stopSelf();
        }

        @Override
        public void onRemoteMediaPlayerStatusUpdated() {
            int mediaStatus = mCastManager.getPlaybackStatus();
            VideoCastNotificationService.this.onRemoteMediaPlayerStatusUpdated(mediaStatus);
        }

    };
    mCastManager.addVideoCastConsumer(mConsumer);
}

From source file:de.mangelow.throughput.NotificationService.java

@Override
public void onCreate() {
    super.onCreate();
    if (D)/* ww w.j  a v  a2s. c  o m*/
        Log.d(TAG, "Service started");

    context = getApplicationContext();
    res = context.getResources();

    if (tmanager == null) {
        tmanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        tmanager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    registerReceiver(mBroadcastReceiver, filter);

    int[] refresh_values = res.getIntArray(R.array.refresh_values);
    long refresh = (long) refresh_values[MainActivity.loadIntPref(context, MainActivity.REFRESH,
            MainActivity.REFRESH_DEFAULT)];

    modifyNotification(R.drawable.ic_stat_zero, null, "", "", new Intent());

    if (handler == null) {
        handler = new Handler();
        handler.postDelayed(mRunnable, refresh);
    }

}

From source file:org.durka.hallmonitor.CoreReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(QUICKBOOT_POWERON)
            || intent.getAction().equals(HTC_QUICKBOOT_POWERON)) {
        Log.d(LOG_TAG + ".boot", "Boot called.");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefs.getBoolean("pref_enabled", false)) {
            Intent mIntent = new Intent(context, CoreService.class);
            context.startService(mIntent);
        }//from  w ww. j  a v  a  2 s . c om
    }

    if (CoreStateManager.getInitialized()) {
        localContext = CoreStateManager.getContext();
        mStateManager = ((CoreApp) localContext).getStateManager();
    } else {
        return;
    }

    if (!mStateManager.getPreference().getBoolean("pref_enabled", false)) {
        return;
    }

    mStateManager.acquireCPUGlobal();

    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

        Log.d(LOG_TAG + ".screen", "Screen on event received.");

        if (mStateManager.getCoverClosed()) {
            Log.d(LOG_TAG + ".screen", "Cover is closed, display Default Activity.");
            mStateManager.setBlackScreenTime(0);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else {
            // Log.d(LOG_TAG + ".screen",
            // "Cover is open, free everything.");
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
        }

    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.d(LOG_TAG + ".screen", "Screen off event received.");
        // mStateManager.freeDevice();

    } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(ALARM_ALERT_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm on event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {

            Log.d(LOG_TAG + ".alarm", "Alarm controls are enabled, taking action.");
            mStateManager.setAlarmFiring(true);

            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_ALARM);
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".alarm", "Alarm controls are not enabled.");
        }

    } else if (intent.getAction().equals(ALARM_DONE_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm done event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {
            Log.d(mStateManager.getPreference() + ".alarm", "alarm is over, cleaning up");
            mStateManager.setAlarmFiring(false);

            if (mStateManager.getCoverClosed()) {
                Intent mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                mStateManager.sendToCoreService(mIntent);
            }
        }
    } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

        if (mStateManager.getPreference().getBoolean("pref_phone_controls", false)) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(LOG_TAG + ".phone", "phone state changed to " + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(true);
                mStateManager.setCallFrom(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
                Log.d(LOG_TAG, "call from " + mStateManager.getCallFrom());
                mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_CALL);
                mStateManager.sendToCoreService(mIntent);
            } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(false);
                Log.d(LOG_TAG, "call is over, cleaning up");
                if (mStateManager.getCoverClosed()) {
                    mIntent = new Intent(localContext, CoreService.class);
                    mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                    mStateManager.sendToCoreService(mIntent);
                }
            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else {
            Log.d(LOG_TAG + ".phone", "phone controls are not enabled");
        }
    } else if (intent.getAction().equals(mStateManager.getActionCover())) {
        int state = intent.getIntExtra(EXTRA_LID_STATE, LID_ABSENT);
        Log.d(LOG_TAG + ".cover", "cover state changed to " + state);
        if (state == LID_CLOSED) {
            Log.d(LOG_TAG + ".cover", "Cover is close, enable Default Activity.");
            mStateManager.setCoverClosed(true);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else if (state == LID_OPEN) {
            // Log.d(LOG_TAG + ".cover",
            // "Cover is open, stopping Default Activity.");
            mStateManager.setCoverClosed(false);
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
            mStateManager.sendToCoreService(mIntent);
        }

    } else if (intent.getAction().equals(TORCH_STATE_CHANGED)) {
        if (mStateManager.getPreference().getBoolean("pref_flash_controls", false)) {
            Log.d(LOG_TAG + ".torch", "torch state changed");
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_TORCH_STATE);
            if (intent.getIntExtra("state", 0) != 0) {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, true);
            } else {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, false);
            }
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".torch", "torch controls are not enabled.");
        }

    } else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getExtras().getInt("state");
        Log.d(LOG_TAG + ".headset", "headset is " + (state == 0 ? "gone" : "here") + "!");
        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_HEADSET_PLUG);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals("org.durka.hallmonitor.debug")) {
        Log.d(LOG_TAG + "", "received debug intent");
        // test intent to show/hide a notification
        boolean showhide = false;
        switch (intent.getIntExtra("notif", 0)) {
        case 1:
            showhide = true;
            break;
        case 2:
            showhide = false;
            break;
        }
        if (showhide) {
            Notification.Builder mBuilder = new Notification.Builder(localContext)
                    .setSmallIcon(R.drawable.ic_launcher).setContentTitle("Hall Monitor")
                    .setContentText("Debugging is fun!");

            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(42, mBuilder.build());
        } else {
            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancel(42);
        }
    }
    mStateManager.releaseCPUGlobal();
}

From source file:name.setup.dance.StepService.java

@Override
public void onCreate() {
    Log.i(TAG, "[SERVICE] onCreate");
    mScorePostModulo = 50;/*from   w  ww.  j  a v a2s. co  m*/
    random = new Random();
    generateScorePostValue();

    super.onCreate();

    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    showNotification();

    // Load settings
    mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    mDanceStepAppSettings = new DanceStepAppSettings(mSettings);
    mState = getSharedPreferences("state", 0);

    mUtils = Utils.getInstance();
    mUtils.setService(this);
    mUtils.initTTS();

    acquireWakeLock();

    // Start detecting
    mStepDetector = new StepDetector();
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    registerDetector();

    // Register our receiver for the ACTION_SCREEN_OFF action. This will make our receiver
    // code be called whenever the phone enters standby mode.
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(mReceiver, filter);

    mStepDisplayer = new StepDisplayer(mDanceStepAppSettings, mUtils);
    mStepDisplayer.setSteps(mSteps = mState.getInt("steps", 0));
    mStepDisplayer.addListener(mStepListener);
    mStepDetector.addStepListener(mStepDisplayer);

    mPaceNotifier = new PaceNotifier(mDanceStepAppSettings, mUtils);
    mPaceNotifier.setPace(mPace = mState.getInt("pace", 0));
    mPaceNotifier.addListener(mPaceListener);
    mStepDetector.addStepListener(mPaceNotifier);

    mDistanceNotifier = new DistanceNotifier(mDistanceListener, mDanceStepAppSettings, mUtils);
    mDistanceNotifier.setDistance(mDistance = mState.getFloat("distance", 0));
    mStepDetector.addStepListener(mDistanceNotifier);

    mSpeedNotifier = new SpeedNotifier(mSpeedListener, mDanceStepAppSettings, mUtils);
    mSpeedNotifier.setSpeed(mSpeed = mState.getFloat("speed", 0));
    mPaceNotifier.addListener(mSpeedNotifier);

    mCaloriesNotifier = new CaloriesNotifier(mCaloriesListener, mDanceStepAppSettings, mUtils);
    mCaloriesNotifier.setCalories(mCalories = mState.getFloat("calories", 0));
    mStepDetector.addStepListener(mCaloriesNotifier);

    mSpeakingTimer = new SpeakingTimer(mDanceStepAppSettings, mUtils);
    mSpeakingTimer.addListener(mStepDisplayer);
    mSpeakingTimer.addListener(mPaceNotifier);
    mSpeakingTimer.addListener(mDistanceNotifier);
    mSpeakingTimer.addListener(mSpeedNotifier);
    mSpeakingTimer.addListener(mCaloriesNotifier);
    mStepDetector.addStepListener(mSpeakingTimer);

    // Used when debugging:
    // mStepBuzzer = new StepBuzzer(this);
    // mStepDetector.addStepListener(mStepBuzzer);

    // Start voice
    reloadSettings();

    // Tell the user we started.
    Toast.makeText(this, getText(R.string.started), Toast.LENGTH_SHORT).show();

}

From source file:com.zhengde163.netguard.ActivitySettings.java

@Override
protected void onResume() {
    super.onResume();

    // Check if permissions were revoked
    checkPermissions();/*from w  ww.ja  va  2  s .  c  om*/

    // Listen for preference changes
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(this);

    // Listen for interactive state changes
    IntentFilter ifInteractive = new IntentFilter();
    ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
    ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

    if (Util.hasPhoneStatePermission(this)) {
        phone_state = true;
    }
}