一个小案例演示获取手机,sd卡的总内存和可用内存。
案例:
一个 TextView显示
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); 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; } }
获取位置 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) 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(); tv_view.setText(currentPosition); } }