Android系统及当前应用配置操作信息相关的常用API
实际项目开发中,很多情况都要得到当前关于手机系统的一些信息及相关配置,比如监测手机屏幕方向,前后摄像头是否可用,是否开启导航功能
关于系统信息及当前应用程序配置的主要几个类
1、android.content.Context
2、android.content.res.Configuration
3、android.content.pm.ActivityInfo
4、android.app.Activity
5、android.os.Environment
6、android.content.res.Resources
7、android.content.res.AssetManager
8、android.content.pm.PackageManager
......
上述类的作用及关系图:
package com.yihui.commonapi; import java.io.IOException; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Environment; import android.test.AndroidTestCase; import android.util.Log; public class TestSystemInfoAPI extends AndroidTestCase { private static final String TAG = "TestSystemInfoAPI"; public void testGetSystemInfo() throws IOException, NameNotFoundException{ Context context = getContext(); /* 当前app应用包、数据存储路径等信息 */ Log.i(TAG, "当前APP包名: " + context.getPackageName()); Log.i(TAG, "当前APP对应的APK所在路径: " + context.getPackageCodePath()); //等同getPackageResourcePath() Log.i(TAG, "当前APP的FilesDir目录: " + context.getFilesDir().getAbsolutePath()); Log.i(TAG, "当前APP的CacheDir目录: " + context.getCacheDir().getAbsolutePath()); Log.i(TAG, "当前APP的SDCard目录: " + Environment.getExternalStorageDirectory().getAbsolutePath()); Log.i(TAG, "当前APP的RootDirectory目录: " + Environment.getRootDirectory().getAbsolutePath()); Log.i(TAG, "当前APP的DataDirectory目录: " + Environment.getDataDirectory().getAbsolutePath()); Log.i(TAG, "当前APP的DownloadCacheDirectory目录: " + Environment.getDownloadCacheDirectory().getAbsolutePath()); /* 当前应用APK管理信息,得到AndroidManifest.xml里面的配置及各个组件信息 */ PackageManager appPackageManager = context.getPackageManager(); PackageInfo appPackageInfo = appPackageManager.getPackageInfo(context.getPackageName(), 0); Log.i(TAG, "当前APP应用的版本信息: " + appPackageInfo.versionCode); /* res资源 */ Resources appResources = context.getResources(); //得到手机屏幕的密度值 Log.i(TAG, "当前手机屏幕密度值: " + appResources.getDisplayMetrics().density); //通过res下面的资源对应的id得到对应的资源文件,类似的还可以得到color、Animation、Dimension、Values Drawable resDrawble = appResources.getDrawable(R.drawable.ic_launcher); //得到手机configuration一些配置信息 Configuration appConfiguration = appResources.getConfiguration(); Log.i(TAG, "当前手机屏幕方向:" + (appConfiguration.orientation == Configuration.ORIENTATION_PORTRAIT ? "竖屏" : "横屏")); /* assets资源:读取assets/music目录下面的文件列表 */ AssetManager appAssets = context.getAssets(); String[] assetsFiles = appAssets.list("music"); for(int i = 0 ; i < assetsFiles.length ; i++){ Log.i(TAG, "assetsFiles" + i + ": "+ assetsFiles[i]); } } }
demo:得到系统的configuration信息,横竖屏幕的切换
布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btnSystemConfig" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取系统配置信息"/> <TextView android:id="@+id/tvInfor" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#6666ffff"/> </LinearLayout>
示例代码:MainActivity.java
package com.yihui.commonapi; import android.os.Bundle; import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; private TextView tvInfor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvInfor = (TextView) findViewById(R.id.tvInfor); findViewById(R.id.btnSystemConfig).setOnClickListener(this); } @Override public void onClick(View v) { StringBuffer sbSystemInfo = new StringBuffer(); Configuration cfg = getResources().getConfiguration(); sbSystemInfo.append("手机系统键盘是否可用: " + cfg.keyboardHidden + "\n") .append("系统键盘类型: " + cfg.keyboard + "\n") .append("用户当前Local: " + cfg.locale + "\n") .append("手机信号国家码: " + cfg.mcc + "\n") .append("手机信号网络码: " + cfg.mnc + "\n") .append("手机触摸屏方式: " + cfg.touchscreen + "\n") .append("手机屏幕方向: " + cfg.orientation + "\n") .append("手机是否开启导航: " + cfg.navigation + "\n"); Log.i(TAG, sbSystemInfo.toString()); tvInfor.setText(sbSystemInfo); /* 手机横竖屏切换 */ if(cfg.orientation == Configuration.ORIENTATION_PORTRAIT){ //如果当前是竖屏,就切换成横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }else{ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //否则切换成横屏 } } }
因为横竖屏幕切换及屏幕尺寸改变,默认情况下会影响Activity的生命周期
所以为了使屏幕切换&屏幕大小改变不影响Activity的生命周期,需要在AndroidManifest.xml清单文件中配置一下activity的android:configChanges属性
<!-- 为activity配置android:configChanges属性,表示该配置不影响该activity的生命周期 --> <activity android:name="com.yihui.commonapi.MainActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
运行效果:
http://blog.csdn.net/yihuiworld