Highlights of various issues in Android development [1-10]

Source: Internet
Author: User

This article mainly collects some problems encountered by individuals during Android Application Development, including how to solve some bugs encountered during development, or how to call a method through code to implement certain functions ..... and other issues. As the saying goes: Good memory is worse than bad writing.
[1] How to click the Button in Android to implement the BACK (return) function:
There are many people on the Internet who say that by calling:
[Html]
OnKeyDown (KeyEvent. KEYCODE_BACK, null );

To implement this function. However, an error is reported !!
You can call the following system methods to implement this function:
[Html]
OnBackPressed ();

[2] How to click the Button (or other methods) in Android to implement the MENU function:
[Html]
OpenOptionsMenu ();
[3] android. view. WindowManager $ BadTokenException: Unable to add window -- token null is not for an application exception solution:
Reason: A Context variable, such as private Context mContext, is defined, and mContext = getApplicationContext () is used in onCreate (Bundle savedInstanceState. At the same time, add an AlertDialog prompt dialog box in the Activity and instantiate it as follows:
[Html]
AlertDialog. Builder builder = new AlertDialog. Builder (mContext );
In this case, an exception occurs! This error is reported in the new AlertDialog. builder (mcontext), although the parameter here is AlertDialog. builder (Context context), but we cannot use the Context obtained by getApplicationContext (). Instead, we must use Activity, because only one Activity can add a form.
Solution: use Activity. this (Activity is the name of your Activity) to fill the parameters in new AlertDialog. Builder (Context context) to create a correct Dialog.
[4] How to dynamically enable full screen and exit full screen in android:
[Html]
/**
* Dynamic full screen setting
*/
Private void setFullScreen (){
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
}
 
/**
* Cancel full screen dynamically
*/
Private void quitFullScreen (){
Final WindowManager. LayoutParams attrs = getWindow (). getAttributes ();
Attrs. flags & = (~ WindowManager. LayoutParams. FLAG_FULLSCREEN );
GetWindow (). setAttributes (attrs );
GetWindow (). clearFlags (WindowManager. LayoutParams. FLAG_LAYOUT_NO_LIMITS );
}
[5] How to obtain the SDCard directory path in Android:
[Html]
/**
* Get the directory path of SDCard
* @ Return
*/
Private String getSDCardPath (){
File sdcardDir = null;
// Determine whether an SDCard exists
Boolean sdcardExist = Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED );
If (sdcardExist ){
SdcardDir = Environment. getExternalStorageDirectory ();
}
Return sdcardDir. toString ();
}
[6] To be continued...

From Android-Idea

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.