一个小案例演示获取手机,sd卡的总内存和可用内存。

案例:

  1. 一个 TextView显示
  2. MainActivity
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
public class MainActivity extends AppCompatActivity {
private TextView text;

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

text = (TextView)findViewById(R.id.tv_text);

//获得sd卡的内存状态
File sdFile = Environment.getExternalStorageDirectory();
String sdcardMemory = getMemoryIfno(sdFile);

//获得手机的内存状态
File file = Environment.getDataDirectory();
String dataMemory = getMemoryIfno(file);

text.setText("sd卡:" + sdcardMemory + "\n" + "手机内存:" +dataMemory);

}

private String getMemoryIfno(File path){

//获得磁盘状态对象 ,并传入路径
StatFs statFs = new StatFs(path.getPath());
long blookSize = statFs.getBlockSizeLong(); //获得单个磁盘扇区大小
long blockCount = statFs.getBlockCountLong();//获得扇区总数
long availableBlock = statFs.getAvailableBlocksLong();//获得可用扇区数量

String totalMeory = Formatter.formatFileSize(this, blookSize * blockCount); //总空间
String avaiableMemory = Formatter.formatFileSize(this, blookSize * availableBlock); //可用空间



return "总共内存为" +totalMeory + "\n" + "可用内存为" + avaiableMemory;

}
}

image.png

获取位置
Android 中获取用户地理位置的操作。

1
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
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

public class MainActivity extends AppCompatActivity {
private TextView tv_view ;
private LocationManager locationManager ;
private String provider ;

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

tv_view = (TextView) findViewById(R.id.tv_view);

//获取LocationManager实例
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

//获取可用的位置提供器
List<String> providerList = locationManager.getProviders(true);

if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (provider.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(this, "没有位置提供可用", Toast.LENGTH_SHORT).show();

return;
}

Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {

//显示当前设备位置信息
showLocation(location);
}
//位置改变改的方法
locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);

}

@Override
protected void onDestroy() {
super.onDestroy();
if(locationManager != null) {
locationManager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {

//显示当前设备位置信息
showLocation(location);

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};
private void showLocation(Location location){
String currentPosition = "latitude is" +location.getLatitude() +"\n" +"longitude is" +location.getLongitude();

//显示在TextView上
tv_view.setText(currentPosition);
}
}