Common Android function code blocks and android code

Source: Internet
Author: User

Common Android function code blocks and android code

1. Set the activity to be untitled and full screen

// Set to requestWindowFeature (Window. FEATURE_NO_TITLE) without a title bar; // set to full screen mode getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );

2. Obtain the screen height and width.

// Obtain the screen height and width. Use the WindowManager class WindowManager wm = (WindowManager) getContext (). getSystemService (Context. WINDOW_SERVICE); int width = wm. getdefadisplay display (). getWidth (); int height = wm. getdefadisplay display (). getHeight ();

3. obtain various mobile phone information

TelephonyManager tm = (TelephonyManager) this. getSystemService (Context. TELEPHONY_SERVICE); String imei = tm. getDeviceId (); // international mobile device identification code String imsi = tm. getSubscriberId (); // International Mobile User identification code String tel = tm. getLine1Number (); // The telephone number String model = android. OS. build. MODEL; // mobile phone MODEL String sdk = android. OS. build. VERSION. SDK; // SDK version String release = android. OS. build. VERSION. RELEASE; // System Version // identify the mobile supplier public String ge Based on the IMSI number TProvidersName (String IMSI) {String ProvidersName = null; // the first three digits of IMSI are countries, followed by two digits of 00 02 are China Mobile, and 01 are China Unicom, 03 is China Telecom. If (IMSI. startsWith ("46000") | IMSI. startsWith ("46002") {ProvidersName = "China Mobile";} else if (IMSI. startsWith ("46001") {ProvidersName = "China Unicom";} else if (IMSI. startsWith ("46003") {ProvidersName = "China Telecom";} return ProvidersName ;}

4. Use Toast to output a string

public void showToast(String text){    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();}

5. Write a string into the file

// Write a String into the public void writeFile (String str, String path) {File file; FileOutputStream out; try {// create a file File = new file (path); File. createNewFile (); // open the output stream of the file out = new FileOutputStream (file); // convert the string into a byte array and write it into the file out. write (str. getBytes (); out. close () ;}catch (IOException e ){}}

6. Read the file content to a string

// Read the File content to the String public String getFileInfo (String path) {file File; String str = ""; FileInputStream in; try {// inputStream file new File (path); in = new FileInputStream (file); // read the file content into the byte array int length = (int) file. length (); byte [] temp = new byte [length]; in. read (temp, 0, length); str = EncodingUtils. getString (temp, "UTF-8"); in. close () ;}catch (IOException e) {} return str ;}

7. Install, uninstall, and update the program

// Call up the system installation application String fileName = Environment. getExternalStorageDirectory () + apkName; Uri uri = Uri. fromFile (new File (fileName); Intent intent = new Intent (Intent. ACTION_VIEW); intent. setDataAndType (uri, "application/vnd. android. package-archive "); this. startActivity (intent); // call up the system to uninstall the application Uri packageURI = Uri. parse ("package: your. app. id "); Intent intent = new Intent (Intent. ACTION_DELETE, packageURI); startActivity (intent );

8. Click the return key twice to exit

// Step 1: Define a variable to identify whether to exit boolean isExit; // step 2, override the onKeyDown method in Activity @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {exit (); return false;} else {return super. onKeyDown (keyCode, event) ;}// step 3, write an exit method public void exit () {if (! IsExit) {isExit = true; Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); mHandler. sendEmptyMessageDelayed (0, 2000);} else {Intent intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); startActivity (intent); System. exit (0) ;}// Step 4: Write A HandlerHandler mHandler = new Handler () {@ Override public void handleMessage (Message msg) according to the Message in the exit () method) {// TODO Auto-generated method stub super. handleMessage (msg); isExit = false ;}};

 


Android code can be shared

Intent. ACTION_SEND, directly calling this Intent will pop up the share selection dialog box.

A piece of commonly used animation effect code of Android (How to Make the clicked image control fly to a specified position)

The Prime Minister wants to create a new image object.
Then, use the TranslateAnimation animation = new TranslateAnimation (fromXDelta, toXDelta, fromYDelta, toYDelta) function to write the current coordinates and the coordinates to be moved into, and then use animation. setFillAfter (true); let the image stay there. The final image. startAnimation (animation) is enough. I saw QQ 2011 used this mobile animation before.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.