Example usage for android.content Context POWER_SERVICE

List of usage examples for android.content Context POWER_SERVICE

Introduction

In this page you can find the example usage for android.content Context POWER_SERVICE.

Prototype

String POWER_SERVICE

To view the source code for android.content Context POWER_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.os.PowerManager for controlling power management, including "wake locks," which let you keep the device on while you're running long tasks.

Usage

From source file:org.camlistore.UploadService.java

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

    mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mPrefs = new Preferences(getSharedPreferences(Preferences.filename(this.getBaseContext()), 0));

    updateBackgroundWatchers();/*  w w  w.  ja va 2 s.  co  m*/

    startForeground(NOTIFY_ID_FOREGROUND, newNotification());
}

From source file:org.wso2.emm.system.service.api.OTAServerManager.java

public OTAServerManager(Context context) throws MalformedURLException {
    serverConfig = new OTAServerConfig(Build.PRODUCT, context);
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "OTA Wakelock");
    this.context = context;
}

From source file:com.spoiledmilk.ibikecph.map.MapFragmentBase.java

@Override
public void onPause() {
    SMLocationManager.getInstance().removeUpdates();
    this.locationOverlay.disableMyLocation();
    super.onPause();
    PowerManager powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = powerManager.isScreenOn();
    if (!isScreenOn) {
        isReturnFromLock = true;//from  w  ww  . j  a v a 2s .c o  m
        lastScrollX = mapView.getScrollX();
        lastScrollY = mapView.getScrollY();
        LOG.d("SMMapFragment onPause, scroll = " + lastScrollX + ", " + lastScrollY);
    }
}

From source file:com.jtxdriggers.android.ventriloid.VentriloidService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    int id = intent.getExtras().getInt("id");
    ServerAdapter db = new ServerAdapter(this);
    server = db.getServer(id);/*from  ww  w . j a  v  a 2s  .c  o m*/

    items = new ItemData(this);
    queue.clear();

    volumePrefs = getSharedPreferences("VOLUMES" + server.getId(), Context.MODE_PRIVATE);
    passwordPrefs = getSharedPreferences("PASSWORDS" + server.getId(), Context.MODE_PRIVATE);

    wakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VentriloidWakeLock");
    wakeLock.acquire();

    wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL,
            "VentriloidWifiLock");
    wifiLock.acquire();

    disconnect = true;

    r = new Runnable() {
        public void run() {
            timeout = true;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (timeout) {
                        VentriloInterface.logout();
                        sendBroadcast(new Intent(Main.SERVICE_RECEIVER).putExtra("type",
                                (short) VentriloEvents.V3_EVENT_LOGIN_FAIL));
                        Toast.makeText(getApplicationContext(), "Connection timed out.", Toast.LENGTH_SHORT)
                                .show();
                        reconnectTimer = 10;
                        handler.post(r);
                    }
                }
            }, 9500);
            if (VentriloInterface.login(server.getHostname() + ":" + server.getPort(), server.getUsername(),
                    server.getPassword(), server.getPhonetic())) {
                new Thread(new Runnable() {
                    public void run() {
                        while (VentriloInterface.recv())
                            ;
                    }
                }).start();
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        VentriloEventData data = new VentriloEventData();
                        VentriloInterface.error(data);
                        sendBroadcast(new Intent(Main.SERVICE_RECEIVER).putExtra("type",
                                (short) VentriloEvents.V3_EVENT_LOGIN_FAIL));
                        Toast.makeText(getApplicationContext(), bytesToString(data.error.message),
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
            timeout = false;
        }
    };
    handler.post(r);

    return Service.START_NOT_STICKY;
}

From source file:com.android.screenspeak.eventprocessor.ProcessorVolumeStream.java

@SuppressWarnings("deprecation")
public ProcessorVolumeStream(FeedbackController feedbackController, CursorController cursorController,
        DimScreenController dimScreenController, ScreenSpeakService service) {
    if (feedbackController == null)
        throw new IllegalStateException("CachedFeedbackController is null");
    if (cursorController == null)
        throw new IllegalStateException("CursorController is null");
    if (dimScreenController == null)
        throw new IllegalStateException("DimScreenController is null");

    mAudioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);
    mCursorController = cursorController;
    mFeedbackController = feedbackController;

    final PowerManager pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, WL_TAG);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(service);
    mService = service;//www.  jav  a2s  .c om
    mDimScreenController = dimScreenController;
    mPatternDetector = new VolumeButtonPatternDetector();
    mPatternDetector.setOnPatternMatchListener(this);
}

From source file:at.alladin.rmbt.android.test.RMBTService.java

@Override
public void onCreate() {
    Log.d(DEBUG_TAG, "created");
    super.onCreate();

    handler = new Handler();

    // initialise the locks
    loopMode = ConfigHelper.isLoopMode(this);

    // initialise the locks
    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "RMBTWifiLock");
    wakeLock = ((PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "RMBTWakeLock");

    // mNetworkStateIntentReceiver = new BroadcastReceiver() {
    // @Override/* w w  w . j a va2  s  .co m*/
    // public void onReceive(Context context, Intent intent) {
    // if
    // (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION))
    // {
    //
    // final boolean connected = !
    // intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
    // false);
    // if (! connected)
    // stopTest();
    // }
    // }
    // };
    // final IntentFilter networkStateChangedFilter = new IntentFilter();
    // networkStateChangedFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    //
    // registerReceiver(mNetworkStateIntentReceiver,
    // networkStateChangedFilter);
}

From source file:cn.dacas.providers.downloads.DownloadThread.java

/**
 * Executes the download in a separate thread
 *//*from   w w w  . j  a  v a  2s  . c o m*/
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
        wakeLock.acquire();

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = Downloads.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(Constants.TAG, "Aborting request for download " + mInfo.mId + ": " + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
        // exceptions
        Log.w(Constants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
        finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}

From source file:com.kong.zxreader.down.downloads.DownloadThread.java

/**
 * Executes the download in a separate thread
 *//*from   w  w w . j  a va 2  s .  c  o  m*/
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, DownConstants.TAG);
        wakeLock.acquire();

        if (DownConstants.LOGV) {
            Log.v(DownConstants.TAG, "initiating download for " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            Log.i(DownConstants.TAG, "Initiating request for download " + mInfo.mId);
            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (DownConstants.LOGV) {
            Log.v(DownConstants.TAG, "download completed for " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = Downloads.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(DownConstants.TAG, "Aborting request for download " + mInfo.mId + ": " + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
        // exceptions
        Log.w(DownConstants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
        finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}

From source file:com.rocketsingh.biker.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///  w ww  .j  a  va2s. c om
public static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MapActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(AndyConstants.NOTIFICATION_ID, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();

}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

public TaskExecutor(TaskResponse tr, TaskManagerParms tmp, TaskListItem tai, EnvironmentParms sdv) {
    taskListItem = tai;/*from   w w  w . j a v  a 2s  . c  o  m*/
    envParms = sdv;
    taskResponse = tr;
    taskMgrParms = tmp;
    taskResponse.active_group_name = tai.group_name;
    taskResponse.active_task_name = tai.task_name;
    taskResponse.active_event_name = tai.event_name;
    taskResponse.prof_notification = tai.prof_notification;
    taskResponse.task_action_notification = tai.task_action_notification;
    mWakelockTask = ((PowerManager) taskMgrParms.context.getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
    //                      | PowerManager.ON_AFTER_RELEASE
                    , "TaskAutomation-TaskExecutor");
    mWakelockTask.acquire();
}