package com.bwie.yuekao01; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.provider.Settings; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import com.bwie.yuekao01.fragment.GnFragment; import com.bwie.yuekao01.fragment.JsFragment; import com.bwie.yuekao01.fragment.KjFragment; import com.bwie.yuekao01.fragment.TtFragment; public class MainActivity extends FragmentActivity implements View.OnClickListener { private FrameLayout frame; private Button btngn; private Button btntt; private Button btnjs; private Button btnkj; private TtFragment ttFragment; private GnFragment gnFragment; private JsFragment jsFragment; private KjFragment kjFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); boolean netWorkInfo = getNetWorkInfo(getApplicationContext()); if (!netWorkInfo) { final Intent intent = new Intent(); intent.setAction("com.bwie.yuekao01"); intent.putExtra("info", "当前没有可用网络"); sendBroadcast(intent); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("是否前去设置网络"); builder.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent1 = new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent1); } }); builder.setNegativeButton("不了",null); builder.create().show(); } frame = (FrameLayout) findViewById(R.id.frame); btngn = (Button) findViewById(R.id.btngn); btntt = (Button) findViewById(R.id.btntt); btnjs = (Button) findViewById(R.id.btnjs); btnkj = (Button) findViewById(R.id.btnkj); btnkj.setOnClickListener(this); btntt.setOnClickListener(this); btngn.setOnClickListener(this); btnjs.setOnClickListener(this); ttFragment = new TtFragment(); gnFragment = new GnFragment(); jsFragment = new JsFragment(); kjFragment = new KjFragment(); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.frame, ttFragment); transaction.add(R.id.frame, gnFragment); transaction.add(R.id.frame, jsFragment); transaction.add(R.id.frame, kjFragment); transaction.hide(gnFragment); transaction.hide(jsFragment); transaction.hide(kjFragment); transaction.commit(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btntt: FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.hide(gnFragment); transaction.hide(jsFragment); transaction.hide(kjFragment); transaction.show(ttFragment); transaction.commit(); break; case R.id.btngn: FragmentManager manager2 = getSupportFragmentManager(); FragmentTransaction transaction2 = manager2.beginTransaction(); transaction2.hide(ttFragment); transaction2.hide(jsFragment); transaction2.hide(kjFragment); transaction2.show(gnFragment); transaction2.commit(); break; case R.id.btnkj: FragmentManager manager3 = getSupportFragmentManager(); FragmentTransaction transaction3 = manager3.beginTransaction(); transaction3.hide(ttFragment); transaction3.hide(jsFragment); transaction3.hide(gnFragment); transaction3.show(kjFragment); transaction3.commit(); break; case R.id.btnjs: FragmentManager manager4 = getSupportFragmentManager(); FragmentTransaction transaction4 = manager4.beginTransaction(); transaction4.hide(ttFragment); transaction4.hide(kjFragment); transaction4.hide(gnFragment); transaction4.show(jsFragment); transaction4.commit(); break; } } public boolean getNetWorkInfo(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); } }
网络是否可用判断以及Fragment
最新推荐文章于 2021-06-19 15:39:27 发布