Example usage for android.os PowerManager newWakeLock

List of usage examples for android.os PowerManager newWakeLock

Introduction

In this page you can find the example usage for android.os PowerManager newWakeLock.

Prototype

public WakeLock newWakeLock(int levelAndFlags, String tag) 

Source Link

Document

Creates a new wake lock with the specified level and flags.

Usage

From source file:it.baywaylabs.jumpersumo.utility.FTPDownloadImage.java

/**
 * Method auto invoked pre execute the task.<br />
 *//* www  . j  a  va  2 s.  c o m*/
@Override
protected void onPreExecute() {
    super.onPreExecute();
    // take CPU lock to prevent CPU from going off if the user
    // presses the power button during download
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.acquire();
}

From source file:com.automated.taxinow.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///ww  w .  j a  va 2 s.  com
private void generateNotification(Context context, String message) {
    // System.out.println("this is message " + message);
    // System.out.println("NOTIFICATION RECEIVED!!!!!!!!!!!!!!" + 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, MainDrawerActivity.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(0, 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.smart.taxi.GcmIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();//from  w  w w.j  a v  a2  s . c  o m
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle
        String msg = extras.toString();
        /*
         * Filter messages based on message type. Since it is likely that GCM
         * will be extended in the future with new message types, just ignore
         * any message types you're not interested in, or that you don't
         * recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            /*for (int i=0; i<0; i++) {
            Log.i(TAG, "Working... " + (i+1)
                    + "/5 @ " + SystemClock.elapsedRealtime());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
            }*/
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
            // Post notification of received message.
            sendNotification(extras);
            try {

                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                PowerManager.WakeLock wl = pm.newWakeLock(
                        PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
                wl.acquire();
                wl.release();

            } catch (Exception ex) {
                Log.e("Wake lock acquire error:", ex.toString());
            }
            // Log.i(TAG, "Received: " + extras.toString());

        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

From source file:com.mowares.massagerexpressclient.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///w w w. j  av  a2s  .  co  m

private void generateNotification(Context context, String message) {
    // System.out.println("this is message " + message);
    // System.out.println("NOTIFICATION RECEIVED!!!!!!!!!!!!!!" + 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, MainDrawerActivity.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(0, 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:us.dustinj.locationstore.io.LocationExporter.java

public LocationExporter(Context context, HttpStatusResponseHandler responseReceiver) {
    m_context = context;/*from w  w w  .  j a  va  2  s  .c  om*/
    m_responseReceiver = responseReceiver;
    m_appPrefs = AppSettings.GetSettings(context);

    PowerManager pm = (PowerManager) m_context.getSystemService(Context.POWER_SERVICE);
    m_wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
}

From source file:com.haha01haha01.harail.DatabaseDownloader.java

@Override
protected void onHandleIntent(Intent workIntent) {
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);

    NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("HaRail GTFS Database").setContentText("Downloading file")
            .setSmallIcon(android.R.drawable.ic_popup_sync).setProgress(0, 0, true);
    notifyManager.notify(1, builder.build());

    wakeLock.acquire();/*  www  .j  av a 2s .  c om*/
    try {
        String filename = "harail_irw_gtfs_" + Utils.getCurrentDateString() + ".zip";
        File zip_file = new File(
                new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS), filename);
        if (downloadFile(irw_gtfs_server, irw_gtfs_port, "anonymous", "", irw_gtfs_filename, zip_file)) {
            sendFinished(extractDb(zip_file, notifyManager, builder));
        } else {
            sendFinished(false);
        }
        wakeLock.release();
    } catch (IOException e) {
        sendFinished(false);
        builder.setContentText("Download failed").setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setProgress(0, 0, false);
        notifyManager.notify(1, builder.build());
        wakeLock.release();
    }
}

From source file:com.zegoggles.smssync.service.ServiceBase.java

protected synchronized void acquireLocks() {
    if (wakeLock == null) {
        PowerManager pMgr = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = pMgr.newWakeLock(wakeLockType(), TAG);
    }/*from  w ww  . j a va 2 s.  co  m*/
    wakeLock.acquire();

    if (isConnectedViaWifi()) {
        // we have Wifi, lock it
        WifiManager wMgr = getWifiManager();
        if (wifiLock == null) {
            wifiLock = wMgr.createWifiLock(getWifiLockType(), TAG);
        }
        wifiLock.acquire();
    }
}

From source file:de.schildbach.wallet.offline.AcceptBluetoothService.java

@Override
public void onCreate() {
    serviceCreatedAt = System.currentTimeMillis();
    log.debug(".onCreate()");

    super.onCreate();
    this.application = (WalletApplication) getApplication();
    final BluetoothAdapter bluetoothAdapter = checkNotNull(BluetoothAdapter.getDefaultAdapter());
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.acquire();/*ww  w . j  a  va2 s  . co m*/

    registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

    try {
        classicThread = new AcceptBluetoothThread.ClassicBluetoothThread(bluetoothAdapter) {
            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
        paymentProtocolThread = new AcceptBluetoothThread.PaymentProtocolThread(bluetoothAdapter) {
            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
    } catch (final IOException x) {
        new Toast(this).longToast(R.string.error_bluetooth, x.getMessage());
        log.warn("problem with listening, stopping service", x);
        CrashReporter.saveBackgroundTrace(x, application.packageInfo());
        stopSelf();
    }

    wallet = new WalletLiveData(application);
    wallet.observe(this, new Observer<Wallet>() {
        @Override
        public void onChanged(final Wallet wallet) {
            classicThread.start();
            paymentProtocolThread.start();
        }
    });
}

From source file:it.technocontrolsystem.hypercontrol.gcm.HCGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *///from w w  w. j av  a2s .c o m
private void sendNotification(Bundle data) {
    String messagetype = data.getString("messagetype");

    switch (messagetype) {

    // crea una notifica che quando cliccata apre il site in allarme
    case "alarm":

        String siteUUID = data.getString("sitenum");
        Site site = DB.getSiteByUUID(siteUUID);

        if (site != null) {

            Intent intent = new Intent();
            intent.setClass(this, SiteActivity.class);
            intent.putExtra("siteid", site.getId());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            String title = "Allarme";
            title += " " + site.getName();

            //                String msg = "";
            //                try {
            //                    int plantnum = Integer.parseInt(data.getString("plantnum"));
            //                    Plant plant = DB.getPlantBySiteAndNumber(siteid, plantnum);
            //                    msg += plant.getName();
            //
            //                    int areanum = Integer.parseInt(data.getString("areanum"));
            //                    Area area = DB.getAreaByIdPlantAndAreaNumber(plant.getId(), areanum);
            //                    msg += " " + area.getName();
            //
            //                } catch (Exception e) {
            //                }

            String msg = data.getString("message");
            Uri defaultSoundUri = Lib.resourceToUri(R.raw.siren);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(msg)
                    .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

            Notification mNotification = notificationBuilder.build();
            mNotification.flags |= Notification.FLAG_INSISTENT;

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0, mNotification);

        } else {
            Log.e(TAG, "site uuid " + siteUUID + " not found");
        }

        break;

    // crea una notifica informativa
    case "info":

        // qui creare la notifica
        // ...

        break;

    }

    // dopo la notifica sveglia il device
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            (PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), getClass().getSimpleName());
    wakeLock.acquire();

}

From source file:com.miljin.setminder.TimerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //keep CPU on while timer is ticking
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");
    wakeLock.acquire();// w  w w. j  a  v a2  s.c  o m

    seconds = intent.getLongExtra(MainActivity.EXTRA_START_TIME, 0);

    Intent getActivityIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getActivityIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //make notification
    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder
            .setContentTitle(getResources().getString(com.miljin.setminder.R.string.notification_title));
    if (seconds >= 120) {
        notificationBuilder.setContentText(Long.toString(seconds / 60)
                + getResources().getString(com.miljin.setminder.R.string.notification_text_plural));
    } else {
        notificationBuilder.setContentText(
                getResources().getString(com.miljin.setminder.R.string.notification_text_minute));
    }
    notificationBuilder.setSmallIcon(com.miljin.setminder.R.drawable.ic_notification);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notifyID, notificationBuilder.build());

    myCountDownTimer = new CountDownTimer(seconds * 1000, 100) {
        public void onTick(long millisUntilFinished) {
            timerServiceTicking = true;
            broadcastTime(millisUntilFinished);
            sendNotification(millisUntilFinished);
        }

        public void onFinish() {
            timerServiceTicking = false;
            broadcastDone();
            wakeLock.release();
        }
    };
    myCountDownTimer.start();

    return START_NOT_STICKY;
}