SettingActivity

来源:互联网 发布:ubuntu怎么dakaiexe 编辑:程序博客网 时间:2024/06/05 16:09

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;@Overrideprotected 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();    }}@Overridepublic 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);        }    }}@Overrideprotected void onDestroy() {    super.onDestroy();    unregisterReceiver(receiver);}

}