Common android code

Source: Internet
Author: User

1. Rotating Images
Bitmap bitmapOrg = BitmapFactory. decodeResource (this. getContext (). getResources (), R. drawable. moon );
Matrix matrix = new Matrix ();
Matrix. postRotate (-90); // Rotation Angle
 
Bitmap resizedBitmap = Bitmap. createBitmap (bitmapOrg, 0, 0,
BitmapOrg. getWidth (), bitmapOrg. getHeight (), matrix, true );
BitmapDrawable BMI = new BitmapDrawable (resizedBitmap );


2. Get the mobile phone number
// Create telephone management

TelephonyManager tm = (TelephonyManager)

// Establish a connection with the mobile phone
Activity. getSystemService (Context. TELEPHONY_SERVICE );

// Obtain the mobile phone number

String phoneId = tm. getLine1Number ();

// Remember to add in manifest file
<Uses-permission
Android: name = "android. permission. READ_PHONE_STATE"/>

// The program cannot be implemented on the simulator. You must connect to the mobile phone.

3. format the string in string. xml
// In strings. xml ..
<String name = "my_text"> Thanks for visiting % s. You age is % d! </String>


// And in the java code:
String. format (getString (R. string. my_text), "oschina", 33 );

4. How to set full screen for android
A. Set in java code
/** Full screen settings to hide all decorations in the window */
RequestWindowFeature (Window. FEATURE_NO_TITLE );
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); B. configure it in AndroidManifest. xml.
<Activity android: name = ". Login. NetEdit" android: label = "@ string/label_net_Edit"
Android: screenOrientation = "portrait" android: theme = "@ android: style/Theme. Black. NoTitleBar. Fullscreen">
<Intent-filter>
<Action android: name = "android. intent. Net_Edit"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>


5. Set Activity to Dialog.
Configure theme for Activity nodes in AndroidManifest. xml as follows:

Android: theme = "@ android: style/Theme. Dialog"

6. Check whether the current network is connected.
ConnectivityManager con = (ConnectivityManager) getSystemService (Activity. CONNECTIVITY_SERVICE );
 
Boolean wifi = con. getNetworkInfo (ConnectivityManager. TYPE_WIFI). isConnectedOrConnecting ();

Boolean internet = con. getNetworkInfo (ConnectivityManager. TYPE_MOBILE). isConnectedOrConnecting (); add permissions in AndroidManifest. xml:

<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>

7. Check whether an Intent is valid
Public static boolean isIntentAvailable (Context context, String action ){
Final PackageManager packageManager = context. getPackageManager ();
Final Intent intent = new Intent (action );
List <ResolveInfo> list =
PackageManager. queryIntentActivities (intent,
PackageManager. MATCH_DEFAULT_ONLY );
Return list. size ()> 0;
}

8. android dialing
Try {
Intent intent = new Intent (Intent. ACTION_CALL );
Intent. setData (Uri. parse ("tel: + 110 "));
StartActivity (intent );
} Catch (Exception e ){
Log. e ("SampleApp", "Failed to invoke call", e );
}

9. Send Email in android
Intent I = new Intent (Intent. ACTION_SEND );
// I. setType ("text/plain"); // use this line for the simulator
I. setType ("message/rfc822"); // use this line on a real machine
I. putExtra (Intent. EXTRA_EMAIL, new String [] {"test@gmail.com", "test@163.com });
I. putExtra (Intent. EXTRA_SUBJECT, "subject goes here ");
I. putExtra (Intent. EXTRA_TEXT, "body goes here ");
StartActivity (Intent. createChooser (I, "Select email application ."));


10. Open a browser in android
Intent viewIntent = new
Intent ("android. intent. action. VIEW", Uri. parse ("http://vaiyanzi.cnblogs.com "));

StartActivity (viewIntent );


11. android obtains the unique identifier of a device.
String android_id = Secure. getString (getContext (). getContentResolver (), Secure. ANDROID_ID );


12. obtain IP addresses in android
Public String getLocalIpAddress (){
Try {
For (Enumeration <NetworkInterface> en = NetworkInterface. getNetworkInterfaces ();
En. hasMoreElements ();){
NetworkInterface intf = en. nextElement ();
For (Enumeration <InetAddress> enumIpAddr = intf. getInetAddresses ();
EnumIpAddr. hasMoreElements ();){
InetAddress inetAddress = enumIpAddr. nextElement ();
If (! InetAddress. isLoopbackAddress ()){
Return inetAddress. getHostAddress (). toString ();
}
}
}
} Catch (SocketException ex ){
Log. e (LOG_TAG, ex. toString ());
}
Return null;
}

13. android obtains the memory card path and usage
/** Obtain the memory card path */
File sdcardDir = Environment. getExternalStorageDirectory ();
/** StatFs viewing file system space usage */
StatFs statFs = new StatFs (sdcardDir. getPath ());
/** Block size */
Long blockSize = statFs. getBlockSize ();
/** Total number of blocks */
Long totalBlocks = statFs. getBlockCount ();
/** Number of blocks in use */
Long availableBlocks = statFs. getAvailableBlocks ();

14 Add a new contact to android
Private Uri insertContact (Context context, String name, String phone ){

ContentValues values = new ContentValues ();
Values. put (People. NAME, name );
Uri uri = getContentResolver (). insert (People. CONTENT_URI, values );
Uri numberUri = Uri. withAppendedPath (uri, People. Phones. CONTENT_DIRECTORY );
Values. clear ();

Values. put (Contacts. Phones. TYPE, People. Phones. TYPE_MOBILE );
Values. put (People. NUMBER, phone );
GetContentResolver (). insert (numberUri, values );

Return uri;
}

15. View battery usage
Intent intentBatteryUsage = new Intent (Intent. ACTION_POWER_USAGE_SUMMARY );
StartActivity (intentBatteryUsage );

16. Obtain the process number
ActivityManager mActivityManager = (ActivityManager) this. getSystemService (ACTIVITY_SERVICE); List <ActivityManager. RunningAppProcessInfo> mRunningProcess = mActivityManager. getRunningAppProcesses ();

Int I = 1;
For (ActivityManager. RunningAppProcessInfo amProcess: mRunningProcess ){
Log. e ("homer Application", (I ++) + "PID =" + amProcess. pid + "; processName =" + amProcess. processName );
}

17. View battery usage
Intent intentBatteryUsage = new Intent (Intent. ACTION_POWER_USAGE_SUMMARY );
StartActivity (intentBatteryUsage );

Related Article

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.