public class MainActivity extends AppCompatActivity implements PlatformActionListener ,Handler.Callback {
//这个也是第三方
private static final int MSG_ACTION_CCALLBACK = 0;
private ProgressDialog progressDialog;
DrawerLayout dl;
ListView lv;
ListView lf;
//关于Tab菜单
ViewPager vp;//关连与菜单
TabLayout tabLayout;
List<String> listvp=new ArrayList<String>();//横向条目名字合计
List<Fragment> listf=new ArrayList<Fragment>();
f1 fa1=new f1();
f2 fa2=new f2();
f3 fa3=new f3();
f4 fa4=new f4();
f5 fa5=new f5();
f6 fa6=new f6();
f7 fa7=new f7();
//适配于listview数据
List<String> list=new ArrayList<String>();
//头部
ActionBarDrawerToggle bar;
TextView user;
ImageView useimg;
TextView sdql;
ImageButton imgb;
//频道管理
private List<ChannelBean> listp;
private String jsonStr;
private String DIRsize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user= (TextView) findViewById(R.id.user);
lv= (ListView) findViewById(R.id.lv_Left);
dl = (DrawerLayout) findViewById(R.id.dl);
tabLayout= (TabLayout) findViewById(R.id.tab);
vp= (ViewPager) findViewById(R.id.vp);
lf= (ListView) findViewById(R.id.lv_Left);
sdql= (TextView) findViewById(R.id.sdql);
imgb= (ImageButton) findViewById(R.id.sdhc);
//加载vp界面
vp.setOffscreenPageLimit(7);
//获得TalLayout数据
getTalLayout();
//获得箭头
get_ActionBar();
//获得listview数据
getdata();
//侧拉listview点击事件
getlistlf();
//查找并清理缓存
getClean();
//判断使用网络类型
getnet();
}
public void getnet(){
int net= net_work.getNetWorkStatus(MainActivity.this);
if(net==1) {
Toast.makeText(MainActivity.this, "使用无线网络中", Toast.LENGTH_SHORT).show();
}else
if(net==2) {
Toast.makeText(MainActivity.this, "使用2G网络中", Toast.LENGTH_SHORT).show();
}else
if(net==3) {
Toast.makeText(MainActivity.this, "使用3G网络中", Toast.LENGTH_SHORT).show();
}else
if(net==4) {
Toast.makeText(MainActivity.this, "使用4G网络中", Toast.LENGTH_SHORT).show();
}else
if(net==0) {
Toast.makeText(MainActivity.this, "未知网络", Toast.LENGTH_SHORT).show();
}
}
public void img(View view){
switch (view.getId()){
case R.id.qq_log:
Platform qq = ShareSDK.getPlatform(QQ.NAME);
qq.setPlatformActionListener(this);
qq.SSOSetting(false);
authorize(qq,2);
Toast.makeText(MainActivity.this,"正在使用QQ进行登陆...",Toast.LENGTH_SHORT).show();
break;
case R.id.wx_log:
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
wechat.setPlatformActionListener(this);
wechat.SSOSetting(false);
authorize(wechat, 1);
Toast.makeText(MainActivity.this,"正在使用微信进行登陆...",Toast.LENGTH_SHORT).show();
break;
case R.id.bd_log:
Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
sina.setPlatformActionListener(this);
sina.SSOSetting(false);
authorize(sina, 3);
Toast.makeText(MainActivity.this,"正在使用新浪进行登陆...",Toast.LENGTH_SHORT).show();
break;
}
//短信登陆
LinearLayout ll= (LinearLayout) findViewById(R.id.sms);
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,LogActivity.class));
Toast.makeText(MainActivity.this,"短信登陆",Toast.LENGTH_SHORT).show();
}
});
}
//授权
private void authorize(Platform plat, int type) {
switch (type) {
case 1:
showProgressDialog(getString(R.string.opening_wechat));
break;
case 2:
showProgressDialog(getString(R.string.opening_qq));
break;
case 3:
showProgressDialog(getString(R.string.opening_blog));
break;
}
// if (plat.isValid()) { //如果授权就删除授权资料
// plat.removeAccount();
// }
plat.showUser(null);//授权并获取用户信息
}
//登陆授权成功的回调
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
UIHandler.sendMessage(msg, this); //发送消息
}
//登陆授权错误的回调
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = throwable;
UIHandler.sendMessage(msg, this);
}
//登陆授权取消的回调
@Override
public void onCancel(Platform platform, int i) {
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
UIHandler.sendMessage(msg, this);
}
@Override
public boolean handleMessage(Message msg) {
hideProgressDialog();
switch (msg.arg1) {
case 1: { // 成功
Toast.makeText(MainActivity.this, "授权登陆成功", Toast.LENGTH_SHORT).show();
//获取用户资料
Platform platform = (Platform) msg.obj;
String userId = platform.getDb().getUserId();//获取用户账号
String userName = platform.getDb().getUserName();//获取用户名字
String userIcon = platform.getDb().getUserIcon();//获取用户头像
String userGender = platform.getDb().getUserGender(); //获取用户性别,m = 男, f = 女,如果微信没有设置性别,默认返回null
Toast.makeText(MainActivity.this, "用户信息为--用户名:" + userName + " 性别:" + userGender, Toast.LENGTH_SHORT).show();
//下面就可以利用获取的用户信息登录自己的服务器或者做自己想做的事啦!
user.setText(userName);
//图片加载
useimg= (ImageView) findViewById(R.id.userimg);
asyncLoadImage(userIcon);
}
break;
case 2: { // 失败
Toast.makeText(MainActivity.this, "授权登陆失败", Toast.LENGTH_SHORT).show();
}
break;
case 3: { // 取消
Toast.makeText(MainActivity.this, "授权登陆取消", Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
/**
* 使用异步任务类加载图片
* @param imgPath
*/
private void asyncLoadImage(String imgPath){
new AsyncTask<String,Void,Bitmap>(){
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//主线程执行的
if(bitmap != null) {
useimg.setImageBitmap(bitmap);
}
}
@Override
protected Bitmap doInBackground(String... params) {
//执行耗时任务
try {
//得到图片的地址
String path = params[0];
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//是否使用网络缓存
connection.setUseCaches(true);
int code = connection.getResponseCode();
if(code == 200){
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(imgPath);
}
//显示dialog
public void showProgressDialog(String message) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(message);
progressDialog.setCancelable(true);
progressDialog.show();
}
//隐藏dialog
public void hideProgressDialog() {
if (progressDialog != null)
progressDialog.dismiss();
}
///
//重写该方法,监听actionbar 条目监听(箭头、三个横线)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.photo:
dl.openDrawer(Gravity.RIGHT);
break;
case R.id.city:
CityListActivity.startCityActivityForResult(this);
break;
case R.id.pindao:
if (listp==null){//判断集合中是否已有数据,没有则创建
listp=new ArrayList<ChannelBean>();
//第一个是显示的条目,第二个参数是否显示
listp.add(new ChannelBean("热点",true));
listp.add(new ChannelBean("军事",true));
listp.add(new ChannelBean("八卦",true));
listp.add(new ChannelBean("游戏",true));
listp.add(new ChannelBean("宠物",true));
listp.add(new ChannelBean("汽车",false));
listp.add(new ChannelBean("热卖",false));
listp.add(new ChannelBean("外卖",false));
listp.add(new ChannelBean("视频",false));
listp.add(new ChannelBean("漫画",false));
listp.add(new ChannelBean("头条",false));
listp.add(new ChannelBean("本地",false));
ChannelActivity.startChannelActivity(this,listp);
}else if (jsonStr!=null){//当判断保存的字符串不为空的时候,直接加载已经有了的字符串
ChannelActivity.startChannelActivity(this,jsonStr);
}
break;
}
//如果抽屉打开,点击按钮,就会关闭,反之亦然
return bar.onOptionsItemSelected(item) | super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==ChannelActivity.REQUEST_CODE&&resultCode==ChannelActivity.RESULT_CODE) {
jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
}
}
//设计箭头
public void get_ActionBar() {
bar=new ActionBarDrawerToggle(this,dl,R.string.openD,R.string.openC);
//设置同步状态
bar.syncState();
//设置抽屉开关监听
dl.addDrawerListener(bar);
//标题栏
ActionBar actionBar = getSupportActionBar();
//显示箭头
actionBar.setDisplayHomeAsUpEnabled(true);
}
//重写MENU方法 头像
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inf=getMenuInflater();
inf.inflate(R.menu.itembar,menu);
return super.onCreateOptionsMenu(menu);
}
public void getTalLayout() {
listvp.add("热点新闻");
listvp.add("漫画");
listvp.add("军事");
listvp.add("直播");
listvp.add("科技");
listvp.add("视频");
listvp.add("社会");
listf.add(fa1);
listf.add(fa2);
listf.add(fa3);
listf.add(fa4);
listf.add(fa5);
listf.add(fa6);
listf.add(fa7);
tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE);//设置tab模式,当前为系统默认模式
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(0)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(1)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(2)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(3)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(4)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(5)));
tabLayout.addTab(tabLayout.newTab().setText(listvp.get(6)));
fragmentA fa=new fragmentA(getSupportFragmentManager(),listvp,listf);
vp.setAdapter(fa); //关联viewpage
tabLayout.setupWithViewPager(vp);
}
//Listview数据
public void getdata() {
list.add("我的头条");
list.add("我的话题");
list.add("收藏");
list.add("活动");
list.add("我的");
list.add("版本更新");
//适配器
Mydata md=new Mydata(MainActivity.this,list);
//
lv.setAdapter(md);
//点击条目
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dl.closeDrawer(Gravity.LEFT);
}
});
}
//日夜模式
public void mode(View view) {
// ImageView img1,img2;
// img1= (ImageView) findViewById(R.id.dy1);
// img2= (ImageView) findViewById(R.id.dy);
switch (view.getId()){
case R.id.dy:
//夜间
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
Toast.makeText(MainActivity.this, "夜间模式", Toast.LENGTH_SHORT).show();
break;
case R.id.dy1:
//日间
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
Toast.makeText(MainActivity.this, "日间模式", Toast.LENGTH_SHORT).show();
break;
}
}
public void getlistlf() {
lf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(list.get(position).equals("版本更新")){
new AlertDialog.Builder(MainActivity.this).setTitle("是否升级?")//设置对话框标题
.setMessage("准备更新软件")//设置显示的内容
.setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
// TODO Auto-generated method stub
VersionUpdateConfig.getInstance()//获取配置实
.setContext(MainActivity.this)//设置上下文
.setDownLoadURL("http://gdown.baidu.com/data/wisegame/65f486476dcc3567/jinritoutiao_634.apk")//设置文件下载链接
.setNotificationTitle("版本升级Demo")//设置通知标题
.startDownLoad();//开始下载
Toast.makeText(MainActivity.this, "开始更新...", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消",new DialogInterface.OnClickListener() {//添加返回按钮
@Override
public void onClick(DialogInterface dialog, int which) {//响应事件
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "请尽快更新!!!", Toast.LENGTH_SHORT).show();
}
}).show();//在按键响应事件中显示此对话框
}
}
});
}
public void getClean() {
//Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
final File fileDie=MainActivity.this.getFilesDir();
try {
DIRsize= DataCleanManager.getCacheSize(fileDie);
sdql.setText("缓存大小:"+DIRsize);
} catch (Exception e) {
e.printStackTrace();
}
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataCleanManager.cleanApplicationData(MainActivity.this,fileDie.getAbsolutePath());
Toast.makeText(MainActivity.this, "已清理!", Toast.LENGTH_SHORT).show();
try {
DIRsize= DataCleanManager.getCacheSize(fileDie);
sdql.setText("缓存大小:"+DIRsize);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}