Android中判断Wift是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏
2017-07-28 10:10
671 查看
第一个问题:判断手机当前上网用的是sim卡还是wifi,我写了一个封装的方法,以后可以拿来用:
[java] view
plain copy
print?
/**
* check the internet is
* mobile or wifi
* add by wangxianming
* in 2012-03-22
*/
private boolean checkWifi() {
boolean isWifiConnect = true;
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
[java] view
plain copy
print?
//check the networkInfos numbers
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是否连接
Java代码
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;
}
第二个例子:判断当前的手机屏幕是否开启了旋转屏幕这个选项:
[java] view
plain copy
print?
/**
* ACCELEROMETER_ROTATION---->explain:
*
* Control whether the accelerometer will be
* used to change screen orientation.
* If 0, it will not be used unless explicitly
* requested by the application;
* if 1, it will be used by default
* unless explicitly disabled by the application.
* Constant Value: "accelerometer_rotation"
*/
systemGravity = Settings.System.getInt(this
.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);//1 is open;0 is close;
第三个是在代码中注册监听内存卡状态的广播:
[java] view
plain copy
print?
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()方法中释放。
在代码中写个内部类的广播:
[java] view
plain copy
print?
<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>
第四个是全屏的设置:写一个简单的方法中;
[java] view
plain copy
print?
<span style="color:#000000;FONT-SIZE: 16px"> //set the activity is fullScreen
private void setFullScreen() {
misFullscreen = !misFullscreen;
if (misFullscreen) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
[java] view
plain copy
print?
/**
* check the internet is
* mobile or wifi
* add by wangxianming
* in 2012-03-22
*/
private boolean checkWifi() {
boolean isWifiConnect = true;
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
[java] view
plain copy
print?
//check the networkInfos numbers
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是否连接
Java代码
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;
}
第二个例子:判断当前的手机屏幕是否开启了旋转屏幕这个选项:
[java] view
plain copy
print?
/**
* ACCELEROMETER_ROTATION---->explain:
*
* Control whether the accelerometer will be
* used to change screen orientation.
* If 0, it will not be used unless explicitly
* requested by the application;
* if 1, it will be used by default
* unless explicitly disabled by the application.
* Constant Value: "accelerometer_rotation"
*/
systemGravity = Settings.System.getInt(this
.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);//1 is open;0 is close;
第三个是在代码中注册监听内存卡状态的广播:
[java] view
plain copy
print?
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()方法中释放。
在代码中写个内部类的广播:
[java] view
plain copy
print?
<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>
第四个是全屏的设置:写一个简单的方法中;
[java] view
plain copy
print?
<span style="color:#000000;FONT-SIZE: 16px"> //set the activity is fullScreen
private void setFullScreen() {
misFullscreen = !misFullscreen;
if (misFullscreen) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
相关文章推荐
- Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)
- Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)
- Android中让应用程序自动安装到手机内存及判断应用程序是否安装在SDCard中
- Android中让应用程序自动安装到手机内存及判断应用程序是否安装在SDCard中
- (转)Android中让应用程序自动安装到手机内存及判断应用程序是否安装在SDCard中
- 判断Android手机是否开启USB调试
- Android怎样判断当前屏幕是否为横屏状态
- 【Android小知识点】判断手机是否开启了模拟位置
- 【Android】ListView监听上下滑动(设置滚动监听判断ListView的滚动方向同时获取屏幕高度、ListView实际高度,判断是否需要展示返回顶部按钮(具体逻辑请看代码--附有详细注释)。
- android端判断手机是否有网,并且无网时跳转到设置界面
- 判断SDCARD扫描状态和是否存在,网络是否可用
- android中判断gps是否开启,通过dialog跳转至设置
- 判断Android的WIFI与GPS状态,并引导用户前去开启GPS与WIFI设置
- Android 判断程序在手机中是否是活动状态或者正在运行状态
- Android中手机的网络状态(判断是否联网/3G/4G/WIFI)
- android 判断 sdcard 是否是 emulated
- Android 工具类 —— 判断网络连接状态、GPS是否打开、基站信息和手机IMSI码
- Android 判断应用程序获取通知栏权限是否开启,以及如何跳转到应用程序设置界面
- [AndroidTips]如何判断手机屏幕是否Secure Lock
- Android横屏竖屏判断--全屏设置--取屏幕宽高