Example usage for android.net ConnectivityManager RESTRICT_BACKGROUND_STATUS_ENABLED

List of usage examples for android.net ConnectivityManager RESTRICT_BACKGROUND_STATUS_ENABLED

Introduction

In this page you can find the example usage for android.net ConnectivityManager RESTRICT_BACKGROUND_STATUS_ENABLED.

Prototype

int RESTRICT_BACKGROUND_STATUS_ENABLED

To view the source code for android.net ConnectivityManager RESTRICT_BACKGROUND_STATUS_ENABLED.

Click Source Link

Document

Device is restricting metered network activity while application is running on background.

Usage

From source file:com.irccloud.android.IRCCloudApplicationBase.java

public void onPause(final Activity context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (notifierSockerTimerTask != null)
        notifierSockerTimerTask.cancel();
    notifierSockerTimerTask = new TimerTask() {
        @Override//w  w w . j ava  2s . c o m
        public void run() {
            notifierSockerTimerTask = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
                    if (info.processName.equals(context.getPackageName())
                            && info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                        return;
                }
            }
            if (!conn.notifier && conn.getState() == NetworkConnection.STATE_CONNECTED) {
                conn.disconnect();
                if (ServersList.getInstance().count() < 1) {
                    Crashlytics.log(Log.DEBUG, "IRCCloud",
                            "No servers configured, not connecting notifier socket");
                    return;
                }
                if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered() && cm
                        .getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED)) {
                    try {
                        Thread.sleep(1000);
                        conn.notifier = true;
                        conn.connect();
                    } catch (Exception e) {
                    }
                }
            }
        }
    };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered()
            && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED) {
        notifierTimer.schedule(notifierSockerTimerTask, 5000);
    } else {
        notifierTimer.schedule(notifierSockerTimerTask, 300000);
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

@TargetApi(Build.VERSION_CODES.N)
public static boolean dataSaving(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
}