android 获取手机设备相关信息

在开发中,会时不时的用到手机设备相关数据,趁着现在有点时间,然后加上度娘一把,总结一下在开发中平时使用到相关手机设备的数据,供之后开发便利

涉及到的有

手机sim卡信息{method1~method3} 【备:method指的是对应下面的方法名】

手机型号{method4}

屏幕尺寸大小{method5}

系统当前时间{method6}

sd卡是否存在{method7}

sd总空间和剩余空间{method8}

网络相关状态{method9}

GPS状态{method10}

是否是第一次打开apk{method11}

获取当前包的版本号{method12}


MainActivity代码

public class MainActivity extends Activity implements View.OnClickListener {
    private TelephonyManager telephonyManager;
    private Button btn1;
    private TextView tv1;
    private Button btn2;
    private TextView tv2;
    private Button btn3;
    private TextView tv3;
    private Button btn4;
    private TextView tv4;
    private Button btn5;
    private TextView tv5;
    private Button btn6;
    private TextView tv6;
    private Button btn7;
    private TextView tv7;
    private Button btn8;
    private TextView tv8;
    private TextView tv81;
    private Button btn9;
    private TextView tv9;
    private Button btn10;
    private TextView tv10;
    private Button btn11;
    private TextView tv11;
    private Button btn12;
    private TextView tv12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        lister();
        initData();

    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        btn2 = (Button) findViewById(R.id.btn2);
        tv2 = (TextView) findViewById(R.id.tv2);
        btn3 = (Button) findViewById(R.id.btn3);
        tv3 = (TextView) findViewById(R.id.tv3);
        btn4 = (Button) findViewById(R.id.btn4);
        tv4 = (TextView) findViewById(R.id.tv4);
        btn5 = (Button) findViewById(R.id.btn5);
        tv5 = (TextView) findViewById(R.id.tv5);
        btn6 = (Button) findViewById(R.id.btn6);
        tv6 = (TextView) findViewById(R.id.tv6);
        btn7 = (Button) findViewById(R.id.btn7);
        tv7 = (TextView) findViewById(R.id.tv7);
        btn8 = (Button) findViewById(R.id.btn8);
        tv8 = (TextView) findViewById(R.id.tv8);
        tv81 = (TextView) findViewById(R.id.tv81);
        btn9 = (Button) findViewById(R.id.btn9);
        tv9 = (TextView) findViewById(R.id.tv9);
        btn10 = (Button) findViewById(R.id.btn10);
        tv10 = (TextView) findViewById(R.id.tv10);
        btn11 = (Button) findViewById(R.id.btn11);
        tv11 = (TextView) findViewById(R.id.tv11);
        btn12 = (Button) findViewById(R.id.btn12);
        tv12 = (TextView) findViewById(R.id.tv12);
    }

    private void lister() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
        btn11.setOnClickListener(this);
        btn12.setOnClickListener(this);
    }

    private void initData() {
        telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                method1();
                break;
            case R.id.btn2:
                method2();
                break;
            case R.id.btn3:
                method3();
                break;
            case R.id.btn4:
                method4();
                break;
            case R.id.btn5:
                method5();
                break;
            case R.id.btn6:
                method6();
                break;
            case R.id.btn7:
                method7();
                break;
            case R.id.btn8:
                method8();
                break;
            case R.id.btn9:
                method9();
                break;
            case R.id.btn10:
                method10();
                break;
            case R.id.btn11:
                method11();
                break;
            case R.id.btn12:
                method12();
                break;
        }
    }

    /**
     * 获取手机国际识别码IMEI
     */
    private void method1() {
        tv1.setText("设备ID(IMEI)"+telephonyManager.getDeviceId());
    }

    private void method2() {
        String providerName = "";
        String mIMSI = telephonyManager.getSubscriberId();
        /**
         * IMSI前面三位460是国家号码,其次的两位是运营商代号,
         * 00、02是中国移动,01是联通,03是电信。
         */
        if (mIMSI.startsWith("46000") || mIMSI.startsWith("46002")){
            providerName = "中国移动";
        }else if (mIMSI.startsWith("46001")){
            providerName = "中国联通";
        }else if (mIMSI.startsWith("46003")){
            providerName = "中国电信";
        }
        tv2.setText("IMSI是:"+mIMSI + "  手机服务商:"+providerName);
    }

    /**
     * 获取手机信息
     */
    private void method3() {
        StringBuilder sb=new StringBuilder();
        sb.append("\n设备ID(IMEI)"+telephonyManager.getDeviceId());
        sb.append("\n设备软件版本:"+telephonyManager.getDeviceSoftwareVersion());
        sb.append("\n国家网络Iso:"+telephonyManager.getNetworkCountryIso());
        sb.append("\n网络运营商:"+telephonyManager.getNetworkOperator());
        sb.append("\n网络运营商的名称:"+telephonyManager.getNetworkOperatorName());
        sb.append("\n网络类型:"+telephonyManager.getNetworkType());
        sb.append("\n手机类型:"+telephonyManager.getPhoneType());
        sb.append("\n国际Sim卡Iso:"+telephonyManager.getSimCountryIso());
        sb.append("\nSim卡运营商:"+telephonyManager.getSimOperator());
        sb.append("\nSim卡运营商名称:"+telephonyManager.getSimOperatorName());
        sb.append("\nSim卡序列号:"+telephonyManager.getSimSerialNumber());
        sb.append("\n获取Sim卡状态:"+telephonyManager.getSimState());
        sb.append("\nSubscriberId:"+telephonyManager.getSubscriberId());
        sb.append("\n语音信箱号码:"+telephonyManager.getVoiceMailNumber());
        tv3.setText(sb.toString());
    }

    /**
     * 获取手机型号
     */
    private void method4() {
        Build bd = new Build();
        String model = bd.MODEL;
        tv4.setText("手机型号:"+model);
    }

    /**
     * 获取屏幕尺寸大小
     */
    private void method5() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;   //屏幕宽度(像素)
        int height = metric.heightPixels; //屏幕高度(像素)
        float density = metric.density;   //屏幕密度(比如0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi; // 屏幕密度DPI(如120 / 160 / 240)
        tv5.setText("屏幕尺寸大小 width"+width +" height"+height + " density"+density+ " densityDpi"+densityDpi);
    }

    /**
     * 获取系统当前时间
     */
    private void method6() {
        Date data = new Date();
        tv6.setText("系统当前时间"+data);
    }

    /**
     * 判断SD卡是否存在
     * @return
     */
    private boolean method7() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            tv7.setText("存在");
            return true;
        }else{
            tv7.setText("不存在");
            return false;
        }
    }

    /**
     * SD卡剩余空间
     */
    private void method8() {
       if (method7()){
           // 获得sd卡的内存状态
           File sdcardFileDir = Environment.getExternalStorageDirectory();
           String sdcardMemory = getMemoryInfo(sdcardFileDir);
           tv8.setText("外部存储的容量"+"\n"+sdcardMemory);
       }else {
           Toast.makeText(this,"SD卡不可用,请检查SD卡的状态",Toast.LENGTH_LONG).show();
       }

        // 获得手机内部存储控件的状态
        File dataFileDir = Environment.getDataDirectory();
        String dataMemory = getMemoryInfo(dataFileDir);
        tv81.setText("内部存储的大小"+"\n"+dataMemory);
    }

    /**
     * 根据路径获取内存状态
     * @param path
     * @return
     */
    private String getMemoryInfo(File path) {
        // 获得一个磁盘状态对象
        StatFs stat = new StatFs(path.getPath());

        long blockSize = stat.getBlockSize();   // 获得一个扇区的大小

        long totalBlocks = stat.getBlockCount();    // 获得扇区的总数

        long availableBlocks = stat.getAvailableBlocks();   // 获得可用的扇区数量

        // 总空间
        String totalMemory =  Formatter.formatFileSize(this, totalBlocks * blockSize);
        // 可用空间
        String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize);

        return "总空间: " + totalMemory + "\n可用空间: " + availableMemory;
    }

    /**
     * 网络相关状态
     */
    private void method9() {
        if (NetworkUtils.isNetworkAvailable(this)){
            if (NetworkUtils.isWifi(this)){
                tv9.setText("WIFI");
            }else{
                tv9.setText("3G");
            }
        }else{
            tv9.setText("无网");
        }
    }

    /**
     * GPS是否打开
     */
    private boolean method10() {
        LocationManager locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            tv10.setText("打开");
            return true;
        }
        tv10.setText("关闭");
        return false;
    }

    /**
     * 判断是否是第一次启动apk
     */
    private boolean method11() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        boolean first = pref.getBoolean("key",true);
        if (first){
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("key",false);
            editor.commit();
        }
        tv11.setText(first+"");
        return first;
    }

    /**
     * 获取当前包的版本号
     */
    private void method12() {
        try {
            PackageManager packageManager = this.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(),0);
            int versionCode = packageInfo.versionCode;
            tv12.setText("当前版本号:"+versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

上面的使用的NetworkUtils工具类

public class NetworkUtils {
    
    public final static int NONE = 0;
    public final static int WIFI = 1;
    public final static int MOBILE = 2;
    
    /**
     * 获取当前网络状态(是否可用)
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context){
    	boolean netWorkStatus = false;
    	if(null!=context){
    		ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    		if(connManager.getActiveNetworkInfo()!=null){
    			netWorkStatus = connManager.getActiveNetworkInfo().isAvailable();
    		}
    	}
        return netWorkStatus;
    }
    
    /**
     * 获取3G或者WIFI网络
     * @param context
     * @return
     */
    public static int getNetworkState(Context context){
    	if(null!=context){
    		ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    		State state;
    		NetworkInfo networkInfo;
    		if(null!=connManager){
    			//Wifi网络判断
    			networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    			if(null!=networkInfo){
    				state = networkInfo.getState();
    				if(state == State.CONNECTED||state == State.CONNECTING){
    					return WIFI;
    				}
    			}
    			
    			//3G网络判断
    			networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    			if(null!=networkInfo){
    				state = networkInfo.getState();
    				if(state == State.CONNECTED||state == State.CONNECTING){
    					return MOBILE;
    				}
    			}
        	}
    		
    	}
        return NONE;
    }

	/*
    * 判断是否有wifi连接
    */
	public static boolean isWifi(Context context)
	{
		if (context != null)
		{
			ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo info = connectMgr.getActiveNetworkInfo();

			if(null==info){
				return false ;
			}
			if(info.getType() == ConnectivityManager.TYPE_WIFI ){
				return true ;
			}
		}
		return false;
	}
}


activity_main的布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手机设备识别码"/>
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手机服务商信息"/>
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机信息"/>
        <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手机型号"/>
            <TextView
                android:id="@+id/tv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>


        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="屏幕尺寸大小"/>
        <TextView
            android:id="@+id/tv5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="系统当前时间"/>
            <TextView
                android:id="@+id/tv6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SD是否存在"/>
            <TextView
                android:id="@+id/tv7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SD剩余空间"/>
        <TextView
            android:id="@+id/tv8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"/>
        <TextView
            android:id="@+id/tv81"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="网络相关状态"/>
            <TextView
                android:id="@+id/tv9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="GPS是否打开"/>
            <TextView
                android:id="@+id/tv10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="是否是第一次启动apk"/>
            <TextView
                android:id="@+id/tv11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="当前包的版本号"/>
            <TextView
                android:id="@+id/tv12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"/>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

清单文件相关权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hh.mobiledeviceinformation">
    <!--sim卡权限-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!--内存权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <!--网络权限-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <!--GPS权限-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值