Android手机信息获取

经典好文推荐,通过阅读本文,您将收获以下知识点:
一、 获取手机基本信息(厂商、型号等参数)
二、设备信息获取实现图
三、 获取手机设备 宽、高、IMEI 信息
四、 获取手机厂商名、产品名、手机品牌、手机型号、主板名、设备名
五、获取手机硬件名、SDK版本、android版本 、语言支持、默认语言
六、 获取 SD 卡存储信息
七、 获取手机 RAM、ROM存储信息
八、DeviceInfoUtils 封装类
九、SDCardUtils 封装类
十、参考文献
下面将讲解以上信息的获取方法。
一、 获取手机基本信息(厂商、型号等参数)

以小米手机为例,手机常用的基本信息可以在Settings--> About Phone中看到,
例如下图:

小米手机设备信息图

那么如何获取这些设备信息呢? Android中 通常通过 android.os.Build类方法可以获取更多手机设备信息。

二、 设备信息获取实现图

获取手机IMEI、宽、高、是否有SD卡,RAM、ROM、SD卡、是否联网、网络类型 

默认语言,设备名,型号、厂商、Fingerprint、Android 版本、SDK版本、Google 安全patch、发布时间、版本类型、用户名 

产品名、ID、产品名、主板名

三、 获取手机设备 宽、高、IMEI 信息方法

获取手机宽、高、IMEI信息方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

/**

 * 获取设备宽度(px)

 *

 */

public static int getDeviceWidth(Context context) {

    return context.getResources().getDisplayMetrics().widthPixels;

}

/**

 * 获取设备高度(px)

 */

public static int getDeviceHeight(Context context) {

    return context.getResources().getDisplayMetrics().heightPixels;

}

/**

 * 获取设备的唯一标识, 需要 “android.permission.READ_Phone_STATE”权限

 */

public static String getIMEI(Context context) {

    TelephonyManager tm = (TelephonyManager) context

            .getSystemService(Context.TELEPHONY_SERVICE);

    String deviceId = tm.getDeviceId();

    if (deviceId == null) {

        return "UnKnown";

    else {

        return deviceId;

    }

}

注意: 获取IMEI 需要获取手机状态权限

1

2

<!-- 读取手机IMEI的设备权限 -->

   <uses-permission android:name="android.permission.READ_PHONE_STATE" />

如果是Android 6.0 之后的代码请使用动态申请权限的方法申请权限,否认会报安全异常的错误SecurityException,进而导致运行报错。

如需了解更多 系统安全权限的内容,请看 之前写的文章 Android 系统权限使用详解

四、 获取手机厂商名、产品名、手机品牌、手机型号、主板名、设备名的方法

获取手机厂商名、产品名、手机品牌、手机型号、主板名、设备名的方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

/**

 * 获取厂商名

 * **/

public static String getDeviceManufacturer() {

    return android.os.Build.MANUFACTURER;

}

/**

 * 获取产品名

 * **/

public static String getDeviceProduct() {

    return android.os.Build.PRODUCT;

}

/**

 * 获取手机品牌

 */

public static String getDeviceBrand() {

    return android.os.Build.BRAND;

}

/**

 * 获取手机型号

 */

public static String getDeviceModel() {

    return android.os.Build.MODEL;

}

/**

 * 获取手机主板名

 */

public static String getDeviceBoard() {

    return android.os.Build.BOARD;

}

/**

 * 设备名

 * **/

public static String getDeviceDevice() {

    return android.os.Build.DEVICE;

}

/**

 *

 *

 * fingerprit 信息

 * **/

public static String getDeviceFubgerprint() {

    return android.os.Build.FINGERPRINT;

}

五、 获取手机硬件名、SDK版本、android版本 、语言支持、默认语言等方法

获取手机硬件名、SDK版本、android版本 、语言支持、默认语言等方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

/**

 * 硬件名

 *

 * **/

public static String getDeviceHardware() {

    return android.os.Build.HARDWARE;

}

/**

 * 主机

 *

 * **/

public static String getDeviceHost() {

    return android.os.Build.HOST;

}

/**

 *

 * 显示ID

 * **/

public static String getDeviceDisplay() {

    return android.os.Build.DISPLAY;

}

/**

 * ID

 *

 * **/

public static String getDeviceId() {

    return android.os.Build.ID;

}

/**

 * 获取手机用户名

 *

 * **/

public static String getDeviceUser() {

    return android.os.Build.USER;

}

/**

 * 获取手机 硬件序列号

 * **/

public static String getDeviceSerial() {

    return android.os.Build.SERIAL;

}

/**

 * 获取手机Android 系统SDK

 *

 * @return

 */

public static int getDeviceSDK() {

    return android.os.Build.VERSION.SDK_INT;

}

/**

 * 获取手机Android 版本

 *

 * @return

 */

public static String getDeviceAndroidVersion() {

    return android.os.Build.VERSION.RELEASE;

}

/**

 * 获取当前手机系统语言。

 */

public static String getDeviceDefaultLanguage() {

    return Locale.getDefault().getLanguage();

}

/**

 * 获取当前系统上的语言列表(Locale列表)

 */

public static String getDeviceSupportLanguage() {

    Log.e("wangjie""Local:" + Locale.GERMAN);

    Log.e("wangjie""Local:" + Locale.ENGLISH);

    Log.e("wangjie""Local:" + Locale.US);

    Log.e("wangjie""Local:" + Locale.CHINESE);

    Log.e("wangjie""Local:" + Locale.TAIWAN);

    Log.e("wangjie""Local:" + Locale.FRANCE);

    Log.e("wangjie""Local:" + Locale.FRENCH);

    Log.e("wangjie""Local:" + Locale.GERMANY);

    Log.e("wangjie""Local:" + Locale.ITALIAN);

    Log.e("wangjie""Local:" + Locale.JAPAN);

    Log.e("wangjie""Local:" + Locale.JAPANESE);

    return Locale.getAvailableLocales().toString();

}

六、 获取 SD 卡存储信息

图片描述


SD卡信息

1.判断SD是否挂载方法

判断SD是否挂载方法如下:

1

2

3

4

5

6

7

/**

 * 判断SD是否挂载

 */

public static boolean isSDCardMount() {

    return Environment.getExternalStorageState().equals(

            Environment.MEDIA_MOUNTED);

}

  1. 获取SD 存储信息的方法

获取SD 存储信息的方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/**

     * 获取手机存储 ROM 信息

     *

     * type: 用于区分内置存储于外置存储的方法

     *

     * 内置SD卡 :INTERNAL_STORAGE = 0;

     *

     * 外置SD卡: EXTERNAL_STORAGE = 1;

     * **/

    public static String getStorageInfo(Context context, int type) {

        String path = getStoragePath(context, type);

        /**

         * 无外置SD 卡判断

         * **/

        if (isSDCardMount() == false || TextUtils.isEmpty(path) || path == null) {

            return "无外置SD卡";

        }

        File file = new File(path);

        StatFs statFs = new StatFs(file.getPath());

        String stotageInfo;

        long blockCount = statFs.getBlockCountLong();

        long bloackSize = statFs.getBlockSizeLong();

        long totalSpace = bloackSize * blockCount;

        long availableBlocks = statFs.getAvailableBlocksLong();

        long availableSpace = availableBlocks * bloackSize;

        stotageInfo = "可用/总共:"

                + Formatter.formatFileSize(context, availableSpace) + "/"

                + Formatter.formatFileSize(context, totalSpace);

        return stotageInfo;

    }

  1. 获取手机ROM (内置存储,外置存储)存储路径的方法

获取手机ROM 存储信息的方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

/**

     * 使用反射方法 获取手机存储路径

     *

     * **/

    public static String getStoragePath(Context context, int type) {

        StorageManager sm = (StorageManager) context

                .getSystemService(Context.STORAGE_SERVICE);

        try {

            Method getPathsMethod = sm.getClass().getMethod("getVolumePaths",

                    null);

            String[] path = (String[]) getPathsMethod.invoke(sm, null);

            switch (type) {

            case INTERNAL_STORAGE:

                return path[type];

            case EXTERNAL_STORAGE:

                if (path.length > 1) {

                    return path[type];

                else {

                    return null;

                }

            default:

                break;

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    /**

     * 获取 手机 RAM 信息 方法 一

     * */

    public static String getTotalRAM(Context context) {

        long size = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo outInfo = new MemoryInfo();

        activityManager.getMemoryInfo(outInfo);

        size = outInfo.totalMem;

        return Formatter.formatFileSize(context, size);

    }

    /**

     * 手机 RAM 信息 方法 二

     * */

    public static String getTotalRAMOther(Context context) {

        String path = "/proc/meminfo";

        String firstLine = null;

        int totalRam = 0;

        try {

            FileReader fileReader = new FileReader(path);

            BufferedReader br = new BufferedReader(fileReader, 8192);

            firstLine = br.readLine().split("\\s+")[1];

            br.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

        if (firstLine != null) {

            totalRam = (int) Math.ceil((new Float(Float.valueOf(firstLine)

                    / (1024 * 1024)).doubleValue()));

            long totalBytes = 0;

        }

        return Formatter.formatFileSize(context, totalRam);

    }

    /**

     * 获取 手机 可用 RAM

     * */

    public static String getAvailableRAM(Context context) {

        long size = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo outInfo = new MemoryInfo();

        activityManager.getMemoryInfo(outInfo);

        size = outInfo.availMem;

        return Formatter.formatFileSize(context, size);

    }

七、获取手机 RAM、ROM存储信息

1.RAM:

运行时内存,此大小直接决定手机运行的流畅度,相当于电脑内存。

2.ROM :

手机存储(分内置SD卡,外置SD卡),此大小直接决定着手机可以存储资源的大小,相当于电脑硬盘。

以红米手机为例:
RAM= 1904716KB= 1.82G

图片描述


红米4 手机 RAM、ROM存储信息

图片描述


红米4 memory 信息 meminfo

3.获取 RAM存储信息的方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

     * 获取 手机 RAM 信息

     * */

    public static String getRAMInfo(Context context) {

        long totalSize = 0;

        long availableSize = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo memoryInfo = new MemoryInfo();

        activityManager.getMemoryInfo(memoryInfo);

        totalSize = memoryInfo.totalMem;

        availableSize = memoryInfo.availMem;

        return "可用/总共:" + Formatter.formatFileSize(context, availableSize)

                + "/" + Formatter.formatFileSize(context, totalSize);

    }

  1. 获取手机ROM存储信息的方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/**

     * 获取手机存储 ROM 信息

     *

     * type: 用于区分内置存储于外置存储的方法

     *

     * 内置SD卡 :INTERNAL_STORAGE = 0;

     *

     * 外置SD卡: EXTERNAL_STORAGE = 1;

     * **/

    public static String getStorageInfo(Context context, int type) {

        String path = getStoragePath(context, type);

        /**

         * 无外置SD 卡判断

         * **/

        if (isSDCardMount() == false || TextUtils.isEmpty(path) || path == null) {

            return "无外置SD卡";

        }

        File file = new File(path);

        StatFs statFs = new StatFs(file.getPath());

        String stotageInfo;

        long blockCount = statFs.getBlockCountLong();

        long bloackSize = statFs.getBlockSizeLong();

        long totalSpace = bloackSize * blockCount;

        long availableBlocks = statFs.getAvailableBlocksLong();

        long availableSpace = availableBlocks * bloackSize;

        stotageInfo = "可用/总共:"

                + Formatter.formatFileSize(context, availableSpace) + "/"

                + Formatter.formatFileSize(context, totalSpace);

        return stotageInfo;

    }

八、DeviceInfoUtils 封装类

为了方便查询使用设备信息,小编已经封装成一个Utils类。代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

package com.programandroid.Utils;

import java.util.Locale;

import android.R.string;

import android.content.Context;

import android.telephony.TelephonyManager;

import android.util.Log;

/*

 * DeviceInfoUtils.java

 *

 *  Created on: 2017-11-16

 *      Author: wangjie

 *

 *  Welcome attention to weixin public number get more info

 *

 *  WeiXin Public Number : ProgramAndroid

 *  微信公众号 :程序员Android

 *

 */

public class DeviceInfoUtils {

    /**

     * 获取设备宽度(px)

     *

     */

    public static int getDeviceWidth(Context context) {

        return context.getResources().getDisplayMetrics().widthPixels;

    }

    /**

     * 获取设备高度(px)

     */

    public static int getDeviceHeight(Context context) {

        return context.getResources().getDisplayMetrics().heightPixels;

    }

    /**

     * 获取设备的唯一标识, 需要 “android.permission.READ_Phone_STATE”权限

     */

    public static String getIMEI(Context context) {

        TelephonyManager tm = (TelephonyManager) context

                .getSystemService(Context.TELEPHONY_SERVICE);

        String deviceId = tm.getDeviceId();

        if (deviceId == null) {

            return "UnKnown";

        else {

            return deviceId;

        }

    }

    /**

     * 获取厂商名

     * **/

    public static String getDeviceManufacturer() {

        return android.os.Build.MANUFACTURER;

    }

    /**

     * 获取产品名

     * **/

    public static String getDeviceProduct() {

        return android.os.Build.PRODUCT;

    }

    /**

     * 获取手机品牌

     */

    public static String getDeviceBrand() {

        return android.os.Build.BRAND;

    }

    /**

     * 获取手机型号

     */

    public static String getDeviceModel() {

        return android.os.Build.MODEL;

    }

    /**

     * 获取手机主板名

     */

    public static String getDeviceBoard() {

        return android.os.Build.BOARD;

    }

    /**

     * 设备名

     * **/

    public static String getDeviceDevice() {

        return android.os.Build.DEVICE;

    }

    /**

     *

     *

     * fingerprit 信息

     * **/

    public static String getDeviceFubgerprint() {

        return android.os.Build.FINGERPRINT;

    }

    /**

     * 硬件名

     *

     * **/

    public static String getDeviceHardware() {

        return android.os.Build.HARDWARE;

    }

    /**

     * 主机

     *

     * **/

    public static String getDeviceHost() {

        return android.os.Build.HOST;

    }

    /**

     *

     * 显示ID

     * **/

    public static String getDeviceDisplay() {

        return android.os.Build.DISPLAY;

    }

    /**

     * ID

     *

     * **/

    public static String getDeviceId() {

        return android.os.Build.ID;

    }

    /**

     * 获取手机用户名

     *

     * **/

    public static String getDeviceUser() {

        return android.os.Build.USER;

    }

    /**

     * 获取手机 硬件序列号

     * **/

    public static String getDeviceSerial() {

        return android.os.Build.SERIAL;

    }

    /**

     * 获取手机Android 系统SDK

     *

     * @return

     */

    public static int getDeviceSDK() {

        return android.os.Build.VERSION.SDK_INT;

    }

    /**

     * 获取手机Android 版本

     *

     * @return

     */

    public static String getDeviceAndroidVersion() {

        return android.os.Build.VERSION.RELEASE;

    }

    /**

     * 获取当前手机系统语言。

     */

    public static String getDeviceDefaultLanguage() {

        return Locale.getDefault().getLanguage();

    }

    /**

     * 获取当前系统上的语言列表(Locale列表)

     */

    public static String getDeviceSupportLanguage() {

        Log.e("wange""Local:" + Locale.GERMAN);

        Log.e("wange""Local:" + Locale.ENGLISH);

        Log.e("wangj""Local:" + Locale.US);

        Log.e("wangj""Local:" + Locale.CHINESE);

        Log.e("wange""Local:" + Locale.TAIWAN);

        Log.e("wange""Local:" + Locale.FRANCE);

        Log.e("wange""Local:" + Locale.FRENCH);

        Log.e("wange""Local:" + Locale.GERMANY);

        Log.e("wange""Local:" + Locale.ITALIAN);

        Log.e("wangj""Local:" + Locale.JAPAN);

        Log.e("wange""Local:" + Locale.JAPANESE);

        return Locale.getAvailableLocales().toString();

    }

    public static String getDeviceAllInfo(Context context) {

        return "\n\n1. IMEI:\n\t\t" + getIMEI(context)

        + "\n\n2. 设备宽度:\n\t\t" + getDeviceWidth(context)

        + "\n\n3. 设备高度:\n\t\t" + getDeviceHeight(context)

        + "\n\n4. 是否有内置SD卡:\n\t\t" + SDCardUtils.isSDCardMount()

        + "\n\n5. RAM 信息:\n\t\t" + SDCardUtils.getRAMInfo(context)

        + "\n\n6. 内部存储信息\n\t\t" + SDCardUtils.getStorageInfo(context, 0)

        + "\n\n7. SD卡 信息:\n\t\t" + SDCardUtils.getStorageInfo(context, 1)

        + "\n\n8. 是否联网:\n\t\t" + Utils.isNetworkConnected(context)

        + "\n\n9. 网络类型:\n\t\t" + Utils.GetNetworkType(context)

        + "\n\n10. 系统默认语言:\n\t\t" + getDeviceDefaultLanguage()

        + "\n\n11. 硬件序列号(设备名):\n\t\t" + android.os.Build.SERIAL

        + "\n\n12. 手机型号:\n\t\t" + android.os.Build.MODEL

        + "\n\n13. 生产厂商:\n\t\t" + android.os.Build.MANUFACTURER

        + "\n\n14. 手机Fingerprint标识:\n\t\t" + android.os.Build.FINGERPRINT

        + "\n\n15. Android 版本:\n\t\t" + android.os.Build.VERSION.RELEASE

        + "\n\n16. Android SDK版本:\n\t\t" + android.os.Build.VERSION.SDK_INT

        + "\n\n17. 安全patch 时间:\n\t\t" + android.os.Build.VERSION.SECURITY_PATCH

        + "\n\n18. 发布时间:\n\t\t" + Utils.Utc2Local(android.os.Build.TIME)

        + "\n\n19. 版本类型:\n\t\t" + android.os.Build.TYPE

        + "\n\n20. 用户名:\n\t\t" + android.os.Build.USER

        + "\n\n21. 产品名:\n\t\t" + android.os.Build.PRODUCT

        + "\n\n22. ID:\n\t\t" + android.os.Build.ID

        + "\n\n23. 显示ID:\n\t\t" + android.os.Build.DISPLAY

        + "\n\n24. 硬件名:\n\t\t" + android.os.Build.HARDWARE

        + "\n\n25. 产品名:\n\t\t" + android.os.Build.DEVICE

        + "\n\n26. Bootloader:\n\t\t" + android.os.Build.BOOTLOADER

        + "\n\n27. 主板名:\n\t\t" + android.os.Build.BOARD

        + "\n\n28. CodeName:\n\t\t" + android.os.Build.VERSION.CODENAME

                + "\n\n29. 语言支持:\n\t\t" + getDeviceSupportLanguage();

    }

}

九、SDCardUtils 封装类

为了方便查询使用设备信息,小编已经封装成一个Utils类。代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

package com.programandroid.Utils;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.lang.reflect.Method;

import android.app.ActivityManager;

import android.app.ActivityManager.MemoryInfo;

import android.content.Context;

import android.os.Build;

import android.os.Environment;

import android.os.StatFs;

import android.os.storage.StorageManager;

import android.text.TextUtils;

import android.text.format.Formatter;

/*

 * SDCardUtils.java

 *

 *  Created on: 2017-11-22

 *      Author: wangjie

 *

 *  Welcome attention to weixin public number get more info

 *

 *  WeiXin Public Number : ProgramAndroid

 *  微信公众号 :程序员Android

 *

 */

public class SDCardUtils {

    private static final int INTERNAL_STORAGE = 0;

    private static final int EXTERNAL_STORAGE = 1;

    /**

     * 获取 手机 RAM 信息

     * */

    public static String getRAMInfo(Context context) {

        long totalSize = 0;

        long availableSize = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo memoryInfo = new MemoryInfo();

        activityManager.getMemoryInfo(memoryInfo);

        totalSize = memoryInfo.totalMem;

        availableSize = memoryInfo.availMem;

        return "可用/总共:" + Formatter.formatFileSize(context, availableSize)

                + "/" + Formatter.formatFileSize(context, totalSize);

    }

    /**

     * 判断SD是否挂载

     */

    public static boolean isSDCardMount() {

        return Environment.getExternalStorageState().equals(

                Environment.MEDIA_MOUNTED);

    }

    /**

     * 获取手机存储 ROM 信息

     *

     * type: 用于区分内置存储于外置存储的方法

     *

     * 内置SD卡 :INTERNAL_STORAGE = 0;

     *

     * 外置SD卡: EXTERNAL_STORAGE = 1;

     * **/

    public static String getStorageInfo(Context context, int type) {

        String path = getStoragePath(context, type);

        /**

         * 无外置SD 卡判断

         * **/

        if (isSDCardMount() == false || TextUtils.isEmpty(path) || path == null) {

            return "无外置SD卡";

        }

        File file = new File(path);

        StatFs statFs = new StatFs(file.getPath());

        String stotageInfo;

        long blockCount = statFs.getBlockCountLong();

        long bloackSize = statFs.getBlockSizeLong();

        long totalSpace = bloackSize * blockCount;

        long availableBlocks = statFs.getAvailableBlocksLong();

        long availableSpace = availableBlocks * bloackSize;

        stotageInfo = "可用/总共:"

                + Formatter.formatFileSize(context, availableSpace) + "/"

                + Formatter.formatFileSize(context, totalSpace);

        return stotageInfo;

    }

    /**

     * 使用反射方法 获取手机存储路径

     *

     * **/

    public static String getStoragePath(Context context, int type) {

        StorageManager sm = (StorageManager) context

                .getSystemService(Context.STORAGE_SERVICE);

        try {

            Method getPathsMethod = sm.getClass().getMethod("getVolumePaths",

                    null);

            String[] path = (String[]) getPathsMethod.invoke(sm, null);

            switch (type) {

            case INTERNAL_STORAGE:

                return path[type];

            case EXTERNAL_STORAGE:

                if (path.length > 1) {

                    return path[type];

                else {

                    return null;

                }

            default:

                break;

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    /**

     * 获取 手机 RAM 信息 方法 一

     * */

    public static String getTotalRAM(Context context) {

        long size = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo outInfo = new MemoryInfo();

        activityManager.getMemoryInfo(outInfo);

        size = outInfo.totalMem;

        return Formatter.formatFileSize(context, size);

    }

    /**

     * 手机 RAM 信息 方法 二

     * */

    public static String getTotalRAMOther(Context context) {

        String path = "/proc/meminfo";

        String firstLine = null;

        int totalRam = 0;

        try {

            FileReader fileReader = new FileReader(path);

            BufferedReader br = new BufferedReader(fileReader, 8192);

            firstLine = br.readLine().split("\\s+")[1];

            br.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

        if (firstLine != null) {

            totalRam = (int) Math.ceil((new Float(Float.valueOf(firstLine)

                    / (1024 * 1024)).doubleValue()));

            long totalBytes = 0;

        }

        return Formatter.formatFileSize(context, totalRam);

    }

    /**

     * 获取 手机 可用 RAM

     * */

    public static String getAvailableRAM(Context context) {

        long size = 0;

        ActivityManager activityManager = (ActivityManager) context

                .getSystemService(context.ACTIVITY_SERVICE);

        MemoryInfo outInfo = new MemoryInfo();

        activityManager.getMemoryInfo(outInfo);

        size = outInfo.availMem;

        return Formatter.formatFileSize(context, size);

    }

    /**

     * 获取手机内部存储空间

     *

     * @param context

     * @return 以M,G为单位的容量

     */

    public static String getTotalInternalMemorySize(Context context) {

        File file = Environment.getDataDirectory();

        StatFs statFs = new StatFs(file.getPath());

        long blockSizeLong = statFs.getBlockSizeLong();

        long blockCountLong = statFs.getBlockCountLong();

        long size = blockCountLong * blockSizeLong;

        return Formatter.formatFileSize(context, size);

    }

    /**

     * 获取手机内部可用存储空间

     *

     * @param context

     * @return 以M,G为单位的容量

     */

    public static String getAvailableInternalMemorySize(Context context) {

        File file = Environment.getDataDirectory();

        StatFs statFs = new StatFs(file.getPath());

        long availableBlocksLong = statFs.getAvailableBlocksLong();

        long blockSizeLong = statFs.getBlockSizeLong();

        return Formatter.formatFileSize(context, availableBlocksLong

                * blockSizeLong);

    }

    /**

     * 获取手机外部存储空间

     *

     * @param context

     * @return 以M,G为单位的容量

     */

    public static String getTotalExternalMemorySize(Context context) {

        File file = Environment.getExternalStorageDirectory();

        StatFs statFs = new StatFs(file.getPath());

        long blockSizeLong = statFs.getBlockSizeLong();

        long blockCountLong = statFs.getBlockCountLong();

        return Formatter

                .formatFileSize(context, blockCountLong * blockSizeLong);

    }

    /**

     * 获取手机外部可用存储空间

     *

     * @param context

     * @return 以M,G为单位的容量

     */

    public static String getAvailableExternalMemorySize(Context context) {

        File file = Environment.getExternalStorageDirectory();

        StatFs statFs = new StatFs(file.getPath());

        long availableBlocksLong = statFs.getAvailableBlocksLong();

        long blockSizeLong = statFs.getBlockSizeLong();

        return Formatter.formatFileSize(context, availableBlocksLong

                * blockSizeLong);

    }

    /**

     *

     * SD 卡信息

     * */

    public static String getSDCardInfo() {

        SDCardInfo sd = new SDCardInfo();

        if (!isSDCardMount())

            return "SD card 未挂载!";

        sd.isExist = true;

        StatFs sf = new StatFs(Environment.getExternalStorageDirectory()

                .getPath());

        sd.totalBlocks = sf.getBlockCountLong();

        sd.blockByteSize = sf.getBlockSizeLong();

        sd.availableBlocks = sf.getAvailableBlocksLong();

        sd.availableBytes = sf.getAvailableBytes();

        sd.freeBlocks = sf.getFreeBlocksLong();

        sd.freeBytes = sf.getFreeBytes();

        sd.totalBytes = sf.getTotalBytes();

        return sd.toString();

    }

    public static class SDCardInfo {

        boolean isExist;

        long totalBlocks;

        long freeBlocks;

        long availableBlocks;

        long blockByteSize;

        long totalBytes;

        long freeBytes;

        long availableBytes;

        @Override

        public String toString() {

            return "isExist=" + isExist + "\ntotalBlocks=" + totalBlocks

                    + "\nfreeBlocks=" + freeBlocks + "\navailableBlocks="

                    + availableBlocks + "\nblockByteSize=" + blockByteSize

                    + "\ntotalBytes=" + totalBytes + "\nfreeBytes=" + freeBytes

                    + "\navailableBytes=" + availableBytes;

        }

    }

    // add start by wangjie for SDCard TotalStorage

    public static String getSDCardTotalStorage(long totalByte) {

        double byte2GB = totalByte / 1024.00 / 1024.00 / 1024.00;

        double totalStorage;

        if (byte2GB > 1) {

            totalStorage = Math.ceil(byte2GB);

            if (totalStorage > 1 && totalStorage < 3) {

                return 2.0 + "GB";

            else if (totalStorage > 2 && totalStorage < 5) {

                return 4.0 + "GB";

            else if (totalStorage >= 5 && totalStorage < 10) {

                return 8.0 + "GB";

            else if (totalStorage >= 10 && totalStorage < 18) {

                return 16.0 + "GB";

            else if (totalStorage >= 18 && totalStorage < 34) {

                return 32.0 + "GB";

            else if (totalStorage >= 34 && totalStorage < 50) {

                return 48.0 + "GB";

            else if (totalStorage >= 50 && totalStorage < 66) {

                return 64.0 + "GB";

            else if (totalStorage >= 66 && totalStorage < 130) {

                return 128.0 + "GB";

            }

        else {

            // below 1G return get values

            totalStorage = totalByte / 1024.00 / 1024.00;

            if (totalStorage >= 515 && totalStorage < 1024) {

                return 1 + "GB";

            else if (totalStorage >= 260 && totalStorage < 515) {

                return 512 + "MB";

            else if (totalStorage >= 130 && totalStorage < 260) {

                return 256 + "MB";

            else if (totalStorage > 70 && totalStorage < 130) {

                return 128 + "MB";

            else if (totalStorage > 50 && totalStorage < 70) {

                return 64 + "MB";

            }

        }

        return totalStorage + "GB";

    }

    // add end by wangjie for SDCard TotalStorage

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值