第一个问题:判断手机当前上网用的是sim卡还是wifi,我写了一个封装的方法,以后可以拿来用:
-
-
-
-
-
-
- private boolean checkWifi() {
- boolean isWifiConnect = true;
- ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
-
- NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
- for (int i = 0; i<networkInfos.length; i++) {
- if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
- if(networkInfos[i].getType() == cm.TYPE_MOBILE) {
- isWifiConnect = false;
- }
- if(networkInfos[i].getType() == cm.TYPE_WIFI) {
- isWifiConnect = true;
- }
- }
- }
- return isWifiConnect;
注:判断网络和WIFI是否连接
- public static boolean checkNetworkConnection(Context context)
- {
- final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
-
- final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
-
- if(wifi.isAvailable()||mobile.isAvailable())
- return true;
- else
- return false;
- }
第二个例子:判断当前的手机屏幕是否开启了旋转屏幕这个选项:
-
-
-
-
-
-
-
-
-
-
-
- systemGravity = Settings.System.getInt(this
- .getContentResolver(),
- Settings.System.ACCELEROMETER_ROTATION);
第三个是在代码中注册监听内存卡状态的广播:
- IntentFilter intentFilter=new IntentFilter);
- intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
- intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
- intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
- intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
- registerReceiver(sdcardListener,intentFilter);
有registerReceiver()注册广播,就有unregisterReceiver()方法,他们是成对出现的。
如果在onCreate()方法中注册广播,就在onDestroy()方法中释放。
如果在onResume()方法中注册广播,就在onPause()方法中释放。
在代码中写个内部类的广播:
- <span style="color:#000000;FONT-SIZE: 16px">private final BroadcastReceiver sdcardListener=new BroadcastReceiver() {
-
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(SummaryAppMainActivityActivity.this, R.string.sd_removed, 2000).show();
- }
- };</span>
第四个是全屏的设置:写一个简单的方法中;
- <span style="color:#000000;FONT-SIZE: 16px">
- private void setFullScreen() {
- misFullscreen = !misFullscreen;
- if (misFullscreen) {
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- } else {
- getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
- }
- }