Refine search
@Override public void onStart() { Context appContext = getApplicationContext(); WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE); wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG); wifiLock.acquire(); PowerManager powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wakeLock.acquire(); super.onStart(); }
private static boolean isAppOnForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); if (activityManager == null) return false; List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null) return false; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { if (!pm.isInteractive()) return false; } else { if (!pm.isScreenOn()) return false; } String packageName = context.getApplicationContext().getPackageName(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }
protected boolean isIdleMode(final Context context) { final String packageName = context.getPackageName(); final PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); boolean isIgnoringOptimizations = manager.isIgnoringBatteryOptimizations(packageName); return manager.isDeviceIdleMode() && !isIgnoringOptimizations; }
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); Log.e("screen on.................................", ""+isScreenOn); if(isScreenOn==false) { WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock"); wl.acquire(10000); WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock"); wl_cpu.acquire(10000); }
private boolean checkIdleRequirement(Context context) { if (!isIdleRequired()) { return true; } PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); return Util.SDK_INT >= 23 ? powerManager.isDeviceIdleMode() : Util.SDK_INT >= 20 ? !powerManager.isInteractive() : !powerManager.isScreenOn(); }
@Override protected void onPause() { super.onPause(); // If the screen is off then the device has been locked PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); boolean isScreenOn; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { isScreenOn = powerManager.isInteractive(); } else { isScreenOn = powerManager.isScreenOn(); } if (!isScreenOn) { // The screen has been locked // do stuff... } }
@SuppressLint("Wakelock") @Override public int onStartCommand(Intent intent, int flags, int startId) { String action = intent.getAction(); if (ACTION_STOP_SERVICE.equals(action)) { mWantsToStop = true; if (mWakeLock == null) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, EmulatorDebug.LOG_TAG); mWakeLock.acquire(); WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); mWifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, EmulatorDebug.LOG_TAG); mWifiLock.acquire(); mWakeLock.release(); mWakeLock = null; Uri executableUri = intent.getData(); String executablePath = (executableUri == null ? null : executableUri.getPath()); String[] arguments = (executableUri == null ? null : intent.getStringArrayExtra(EXTRA_ARGUMENTS)); String cwd = intent.getStringExtra(EXTRA_CURRENT_WORKING_DIRECTORY);
public static boolean isIgnoringOptimizations(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powerManager != null) return powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); } return true; }
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = useWakeLock() ? pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG) : null; try { if (wakeLock != null) wakeLock.acquire(); increaseInotifyWatches(); mContext.startService(new Intent(mContext, SyncthingService.class) .setAction(SyncthingService.ACTION_RESTART)); break; default: } finally { if (wakeLock != null) wakeLock.release(); if (process != null) process.destroy();
Log.e("","accountName : " +accountName); Log.e("","message : " +message); Intent startActivity = new Intent(); startActivity.setClass(context, NotificationAlert.class); startActivity.setAction(NotificationAlert.class.getName()); startActivity.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY); mWakeLock.acquire();
if (notification_type.equalsIgnoreCase("3")) { Log.d(TAG, "Notification: " + "Before Meeting"); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); wakeLock.acquire(); KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock"); kl.disableKeyguard(); Intent intentToMain = new Intent(this, MainActivity.class); intentToMain.putExtra("destroy", "destroy"); intentToMain.addFlags(intentToMain.FLAG_ACTIVITY_NEW_TASK); startActivity(intentToMain); }
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName()); mWakeLock.setReferenceCounted(false); final Intent shutdownIntent = new Intent(this, MediaService.class); shutdownIntent.setAction(SHUTDOWN);
boolean wasHeld = false; if (wakeLock != null) { if (wakeLock.isHeld()) { wasHeld = true; wakeLock.release(); if (context.getPackageManager().checkPermission(Manifest.permission.WAKE_LOCK, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm != null) { wakeLock = pm.newWakeLock(mode | PowerManager.ON_AFTER_RELEASE, ExoMediaPlayer.class.getName()); wakeLock.setReferenceCounted(false); } else { Log.e(TAG, "Unable to acquire WAKE_LOCK due to a null power manager");
private void checkAndAskForBatteryOptimization() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE); if (!powerManager.isIgnoringBatteryOptimizations(getPackageName())) { new MaterialDialog.Builder(MainActivity.this).cancelable(false) .title(R.string.battery_optimization_title) .content(R.string.battery_optimization_desc) .onPositive((dialog, which) -> { try { Intent intent = new Intent(); intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(getApplicationContext(), "Unable to open battery optimisation screen. Please add it manually", Toast.LENGTH_LONG).show(); } }) .onNegative((dialog, which) -> { dialog.dismiss(); }) .positiveText(R.string.Continue) .negativeText(R.string.exit) .show(); } } }
private void requestChangeBatteryOptimizations () { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Intent intent = new Intent(); String packageName = getActivity().getPackageName(); PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); if (pm.isIgnoringBatteryOptimizations(packageName)) intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); else { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); } getActivity().startActivity(intent); } }
private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) { if (mWakeLock == null) { Context appContext = context.getApplicationContext(); PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timber headset button"); mWakeLock.setReferenceCounted(false); } if (DEBUG) Log.v(TAG, "Acquiring wake lock and sending " + msg.what); // Make sure we don't indefinitely hold the wake lock under any circumstances mWakeLock.acquire(10000); mHandler.sendMessageDelayed(msg, delay); }
public static boolean isInteractive(Context context) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) return (pm != null && pm.isScreenOn()); else return (pm != null && pm.isInteractive()); }
boolean dontShowAgain = mPreferences.getBoolean("battery_optimization_dont_show_again", false); if (dontShowAgain || mBatteryOptimizationsDialog != null || pm.isIgnoringBatteryOptimizations(getPackageName()) || mBatteryOptimizationDialogDismissed) { return; .setMessage(R.string.dialog_disable_battery_optimization_message) .setPositiveButton(R.string.dialog_disable_battery_optimization_turn_off, (d, i) -> { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + getPackageName())); try { startActivity(intent);
CompatWorkEnqueuer(Context context, ComponentName cn) { super(context, cn); mContext = context.getApplicationContext(); if (mContext.checkPermission(Manifest.permission.WAKE_LOCK, Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_GRANTED) { // Make wake locks. We need two, because the launch wake lock wants to have // a timeout, and the system does not do the right thing if you mix timeout and // non timeout (or even changing the timeout duration) in one wake lock. PowerManager pm = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)); mLaunchWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, cn.getClassName() + ":launch"); mLaunchWakeLock.setReferenceCounted(false); mRunWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, cn.getClassName() + ":run"); mRunWakeLock.setReferenceCounted(false); } else { TinkerLog.w(TAG, "it would be better to grant WAKE_LOCK permission to your app so that tinker can use WakeLock to keep system awake."); mLaunchWakeLock = mRunWakeLock = null; } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PowerManager pm = (PowerManager) this .getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire(); // Put here YOUR code. Intent startAutoSyncService = new Intent(this, MyService.class); this.startService(startAutoSyncService); wl.release(); }