This article summarizes the classic code snippets commonly used in Android development. Share it for your reference, as follows:
1. Picture rotation
Bitmap bitmapOrg = (().getResources(), ); Matrix matrix = new Matrix(); (-90);//The rotation angleBitmap resizedBitmap = (bitmapOrg, 0, 0, (), (), matrix, true); BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
2. Get your mobile phone number
//Create phone managementTelephonyManager tm = (TelephonyManager) //Create a connection with the mobile phone(Context.TELEPHONY_SERVICE); //Get mobile phone numberString phoneId = tm.getLine1Number(); //Remember to add it in the manifest file<uses-permission android:name=".READ_PHONE_STATE" /> //The program cannot be implemented on the simulator, and you must connect to the mobile phone
3. Format strings
// in .. <string name="my_text">Thanks for visiting %s. You age is %d!</string> // and in the java code: (getString(.my_text), "oschina", 33);
4. How to set full screen of Android
A. Set it in java code
/** Full screen setting, hide all decorations in the window */ requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN);
B. Configure in
<activity android:name="." android:label="@string/label_net_Edit" android:screenOrientation="portrait" android:theme="@android:style/"> <intent-filter> <action android:name=".Net_Edit" /> <category android:name="" /> </intent-filter> </activity>
5. Set Activity to Dialog
Configure the Activity node in the following configuration:
android:theme="@android:style/"
6. Check whether the current network is connected
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE); boolean wifi=(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting(); boolean internet=(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
In Add permissions:
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
7. Check whether an Intent is valid
public static boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = (); final Intent intent = new Intent(action); List<ResolveInfo> list = (intent, PackageManager.MATCH_DEFAULT_ONLY); return () > 0; }
8. Android phone number
try { Intent intent = new Intent(Intent.ACTION_CALL); (("tel:+110")); startActivity(intent); } catch (Exception e) { ("SampleApp", "Failed to invoke call", e); }
9. Send Email in Android
Intent i = new Intent(Intent.ACTION_SEND); //("text/plain"); //Please use this line for the emulator("message/rfc822") ; // Use this line on real machine(Intent.EXTRA_EMAIL, new String[]{"test@","test@}); (Intent.EXTRA_SUBJECT,"subject goes here"); (Intent.EXTRA_TEXT,"body goes here"); startActivity((i, "Select email application."));
10. Open the browser in android
Intent viewIntent = new Intent("",("")); startActivity(viewIntent);
11. Android obtains the device's unique identification code
String android_id = (getContext().getContentResolver(), Secure.ANDROID_ID);
12. Get the IP address in android
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = (); ();) { NetworkInterface intf = (); for (Enumeration<InetAddress> enumIpAddr = (); ();) { InetAddress inetAddress = (); if (!()) { return ().toString(); } } } } catch (SocketException ex) { (LOG_TAG, ()); } return null; }
13. Android obtains the memory card path and usage status
/** Get the memory card path */ File sdcardDir=(); /** StatFs depends on file system space usage */ StatFs statFs=new StatFs(()); /** Block's size*/ Long blockSize=(); /** Total Block Number */ Long totalBlocks=(); /** Number of blocks used */ Long availableBlocks=();
14 Add new contacts in android
private Uri insertContact(Context context, String name, String phone) { ContentValues values = new ContentValues(); (, name); Uri uri = getContentResolver().insert(People.CONTENT_URI, values); Uri numberUri = (uri, .CONTENT_DIRECTORY); (); (, .TYPE_MOBILE); (, phone); getContentResolver().insert(numberUri, values); return uri; }
15. Check the battery usage
Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY); startActivity(intentBatteryUsage);
16. Get the process number
ActivityManager mActivityManager = (ActivityManager) (ACTIVITY_SERVICE); List<> mRunningProcess = (); int i = 1; for ( amProcess : mRunningProcess) { ("homer Application", (i++) + " PID = " + + "; processName = " + ); }
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android control usage summary》、《A summary of Android SMS and Telephone Operation Tips"and"Android multimedia operation skills summary (audio, video, recording, etc.)》
I hope this article will be helpful to everyone's Android programming design.