Share a few Android development useful program code

  • 2020-06-12 10:41:27
  • OfStack

[webview loads local html, html and remote URL]


// Open this package asset In the directory index.html file
wView.loadUrl("file:///android_asset/index.html ");
// Open the local sd Inside the card index.html file
wView.loadUrl("content://com.android.htmlfileprovider/sdcard/index.html");
// Open the specified URL the html file
wView.loadUrl("http://m.xxx.net");

[Get screen resolution]


// through WindowManager To obtain  
DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 
System.out.println("heigth : " + dm.heightPixels); 
System.out.println("width : " + dm.widthPixels); 
// through Resources To obtain          
DisplayMetrics dm2 = getResources().getDisplayMetrics(); 
System.out.println("heigth2 : " + dm2.heightPixels); 
System.out.println("width2 : " + dm2.widthPixels);   
// Gets the default resolution of the screen  
Display display = getWindowManager().getDefaultDisplay(); 
System.out.println("width-display :" + display.getWidth()); 
System.out.println("heigth-display :" + display.getHeight());
// through WindowManager To obtain
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println("heigth : " + dm.heightPixels);
System.out.println("width : " + dm.widthPixels);
// through Resources To obtain
DisplayMetrics dm2 = getResources().getDisplayMetrics();
System.out.println("heigth2 : " + dm2.heightPixels);
System.out.println("width2 : " + dm2.widthPixels);
// Gets the default resolution of the screen
Display display = getWindowManager().getDefaultDisplay();
System.out.println("width-display :" + display.getWidth());
System.out.println("heigth-display :" + display.getHeight());

[Remove screen title and full screen display]


// Get rid of the title  
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set up the full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Get rid of the title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set up the full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

[Setting screen direction]

Configure the properties of Activity in the Manifest.xml file


<activity android:name=".AnimateActivity" android:label="@string/app_name"
    android:screenOrientation="landscape"><!-- landscape landscape portrait Vertical screen -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

In the program control,1 generally in Activity onCreate, onDestroy methods, because the system will restart Activity when the screen direction changes. Therefore, it is necessary to save relevant data before Activity is destroyed to facilitate reloading in the next onCreate method and update the layout of the screen


public void onCreate(Bundle savedInstanceState) { 
    // Mandatory landscape  
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    // TODO Update screen layout  
}
public void onDestroy() { 
    if(getRequestedOrientation() ==  ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
        // Save the data  
    }else if(getRequestedOrientation() ==  ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
        // Save the data  
    } 
}

[Access to memory card path and space usage]


// Gets the memory card path
File sdcardDir = Environment.getExternalStorageDirectory();
// StatFs Look at the file system space usage
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(sdcardDir.getPath());
// Block the size
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(sdcardDir.getPath());
Long blockSize = statFs.getBlockSize();
// The total Block The number of
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(sdcardDir.getPath());
Long totalBlocks = statFs.getBlockCount();
// Has been used Block The number of
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(sdcardDir.getPath());
Long availableBlocks = statFs.getAvailableBlocks();

[Soft keyboard for Android control]


InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

[Access to mobile phone number]

Remember to add permissions to manifest file


<uses-permission  android:name="android.permission.READ_PHONE_STATE" />
// Create a call manager to establish a connection with the phone
TelephonyManager tm = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
// Access to cell phone number
String phoneId = tm.getLine1Number();


Related articles: