Detailed explanation and usage skills of five managers in Android

Detailed explanation and usage skills of the five Managers in Android
1. PowerManager
is mainly used to control the power state, set the screen state, and battery standby state
      PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
      Here I need to keep the screen awake for a long time, not be lock screen, so I call WakeLock.
  WakeLock wake = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |powerManager.ON_AFTER_RELEASE, **Activity.class );
       wake.acquire();//Request execution
       related flags:
       PARTIAL_WAKE_LOCK : Keep the CPU running, the screen and keyboard lights may be turned off of.
       SCREEN_DIM_WAKE_LOCK: keep the CPU running, allow to keep the screen displayed but may be grayed out, allow to turn off the keyboard light
       SCREEN_BRIGHT_WAKE_LOCK: keep the CPU running, allow to keep the screen highlighted, allow to turn off the keyboard light
       FULL_WAKE_LOCK: keep the CPU running, keep the screen highlighted, The keyboard light also stays bright.
Permission obtained:
<uses-permission android:name="android.permission.WAKE_LOCK" />
2. WindowManager
calls the window, sets the value of the screenBrightness parameter in the window, but temporarily sets the brightness of the screen brightness, which can only take effect under an activity.
/**
     * Set screen brightness
     */
    private void setWindowScreen(){
    Window localWindow = getWindow(); 
        WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes(); 
        float f = 5 / 255.0F; 
        localLayoutParams.screenBrightness = f; 
        localWindow.setAttributes (localLayoutParams);
    }
Get permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
It is found that it only takes effect under the current activity. It is necessary to change the relevant values ​​under the system file and call the Uri (similar link) method.
/**
  * Save the minimum screen brightness
  * save light state
  **/
public void saveBrightness(ContentResolver resolver) {
    Uri uri = android.provider.Settings.System.getUriFor("screen_brightness"); 
    android.provider.Settings.System.putInt(resolver, "screen_brightness", 0);
    resolver. notifyChange(uri, null);
}
3. WifiManager
is related to unlimited management, similar to getting the name of the wifi link, judging whether the link is connected, switches, etc. are related to wireless.
WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
wifiinfo contains a lot of things, unlimited IP, name, ISSID, physical address and many other information, here I need to get to see if The wireless of the link lab gets the name of the wireless, the name is SSID.
String ssId = info.getSSID();
manager.setWifiEnabled(true);//Open wifi
manager.setWifiEnabled(false);//Close wifi
related permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Fourth, ConnectivityManager
mainly manages network connection related operations.
ConnectivityManager connManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetWorkInfo netinfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
NetWorkInfo includes network connection information, including connection status, network availability and other related operations
int code = netinfo. getState();
if (wifiState == State.CONNECTED || wifiState == State.CONNECTING)
      return 200;// Successful connection
else
      return 500; // Connection failed
Related permissions:
<uses-permission android:name="android. permission.
5. ActivityManager
provides an interface for interacting with all running Activity in the system. The main interface revolves around running process information, task information, service information and so on.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
am.getRunningAppProcesses();
am.getRunningServices();
am.getDeviceConfigurationInfo();
am.killBackgroundProcessed( PackageName);
am.restartPackage(PackageName);


This article was originally created by Lanqiao Software College (http://xueyuan.lanqiao.org). Please indicate the source for reprinting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326503979&siteId=291194637