SettingActivity

package com.bwie.topnewsnormal;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.topnewsnormal.app.MyApplication;
import com.bwie.topnewsnormal.utils.DataCleanManager;
import com.bwie.topnewsnormal.utils.NetUtils;
import com.example.city_picker.CityListActivity;

import cn.jpush.android.api.JPushInterface;

/**
* Created by Shadow on 2017/8/10.
*/

public class SettingActivity extends Activity implements View.OnClickListener {

private LinearLayout noWifiNet;
private ConnectivityBroadcastReceiver receiver;
private TextView tv_xiatu;
private CheckBox jGuangPush;
private ImageView iv_setting_back;
private LinearLayout moreCity;
private LinearLayout clearCache;
private TextView tv_currCache;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_layout);
    noWifiNet = (LinearLayout) findViewById(R.id.noWifiNet);
    moreCity = (LinearLayout) findViewById(R.id.moreCity);
    clearCache = (LinearLayout) findViewById(R.id.clearCache);
    tv_currCache = (TextView) findViewById(R.id.tv_currCache);
    jGuangPush = (CheckBox) findViewById(R.id.jGuangPush);
    jGuangPush.setOnClickListener(this);
    //設置消息推送
    jGuangPush.setChecked(MyApplication.getApp().getSharedPreferences("jpush", MODE_PRIVATE).getBoolean("jp", false));

    checkJP(jGuangPush.isChecked());
    tv_xiatu = (TextView) findViewById(R.id.tv_xiatu);
    tv_xiatu.setText(MyApplication.getApp().getSharedPreferences(NetUtils.SP_NAME, MODE_PRIVATE).getString("xiaotu", "較省流量(智能下圖)"));
    iv_setting_back = (ImageView) findViewById(R.id.iv_setting_back);

    clearCache.setOnClickListener(this);
    moreCity.setOnClickListener(this);
    iv_setting_back.setOnClickListener(this);
    noWifiNet.setOnClickListener(this);
    receiver = new ConnectivityBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(receiver, intentFilter);

    try {
        String cacheSize = DataCleanManager.getTotalCacheSize(this);
        tv_currCache.setText("當前緩存:"+cacheSize);
        //Toast.makeText(this, cacheSize, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.noWifiNet:
            final String[] strings = {"最佳效果(下載大圖)", "較省流量(智能下圖)", "極省流量(不下載圖)"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            int mode = MyApplication.getApp().getSharedPreferences(NetUtils.SP_NAME, MODE_PRIVATE).getInt(NetUtils.PICTURE_LOAD_MODE_KEY, 0);
            builder.setTitle("非Wifi網絡流量");
            builder.setSingleChoiceItems(strings, mode, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MyApplication.getApp().getSharedPreferences(NetUtils.SP_NAME, MODE_PRIVATE).edit().putInt(NetUtils.PICTURE_LOAD_MODE_KEY, which).commit();
                    MyApplication.getApp().getSharedPreferences(NetUtils.SP_NAME, MODE_PRIVATE).edit().putString("xiaotu", strings[which]).commit();
                    tv_xiatu.setText(strings[which]);
                    switch (which) {
                        case 0:
                            Toast.makeText(SettingActivity.this, "which0", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(SettingActivity.this, "which1", Toast.LENGTH_SHORT).show();

                            break;
                        case 2:
                            Toast.makeText(SettingActivity.this, "which2", Toast.LENGTH_SHORT).show();

                            break;
                    }
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", null);
            builder.create().show();
            break;
        case R.id.jGuangPush:
            if (jGuangPush.isChecked()) {
                MyApplication.getApp().getSharedPreferences("jpush", MODE_PRIVATE).edit().putBoolean("jp", true).commit();
            } else {
                MyApplication.getApp().getSharedPreferences("jpush", MODE_PRIVATE).edit().putBoolean("jp", false).commit();
            }


            break;
        case R.id.iv_setting_back:
            //關閉activity
            this.finish();
            break;
        case R.id.moreCity:
            //跳轉到城市列表
            CityListActivity.startCityActivityForResult(this);
            break;
        case R.id.clearCache:
            //清除緩存
            DataCleanManager.clearAllCache(this);
            try {
                String cacheSize = DataCleanManager.getTotalCacheSize(this);
                tv_currCache.setText("當前緩存:"+cacheSize);
                Toast.makeText(this, "清除成功!", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

    }
}

public void checkJP(boolean flag) {
    if (flag) {
        JPushInterface.resumePush(this);
    } else {
        JPushInterface.stopPush(this);
    }

}

public class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {

            boolean isMobileConnectivity = true;

            //如果能走到這,說明網絡已經發生變化
            ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                if (ConnectivityManager.TYPE_WIFI == activeNetworkInfo.getType()) {
                    Toast.makeText(SettingActivity.this, "wifi可用,下載吧", Toast.LENGTH_SHORT).show();
                    isMobileConnectivity = false;
                } else if (ConnectivityManager.TYPE_MOBILE == activeNetworkInfo.getType()) {
                    Toast.makeText(SettingActivity.this, "現在是移動網絡,當心", Toast.LENGTH_SHORT).show();
                    isMobileConnectivity = true;
                    //獲得現在的網絡狀態 是移動網絡,去改變我們的訪問接口

                } else {
                    Toast.makeText(SettingActivity.this, "網絡不可用,請檢查網絡", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(SettingActivity.this, "網絡不可用,請檢查網絡", Toast.LENGTH_SHORT).show();
            }

            //// TODO: 2017/8/10 改變一下網絡狀態
            NetUtils.getinstance().changNetState(isMobileConnectivity);
        }
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章