Android and android Official Website
Directory
Customize two views for drawing, and refresh and repaint them respectively
I divided the screen into the left and right sections and customized drawing classes drawOneView and drawTwoView. They all inherit the View class, where drawOneView is displayed on the left and drawTwoView on the right, in drawOneView, you can use the invalidate () function to re-paint it. However, in drawTwoView, you cannot use the invalidate () function to re-paint the drawTwoView. Why? In addition, when drawOneView is re-painted, how can it lead to drawTwoView re-painting? They should be controlled by their respective classes. Why?
One uses Invalidate () and the other uses postInvalidate () to refresh.
You can call Invalidate () and postInvalidate () to refresh the UI.
Set Activity to singleTop and press the Home Key for startup.
If the Activity is set to singleTop, press the Home Key and then press the start button for a long time. In the startup mechanism of the Home key, it seems that the Activity set to singleTop will be restarted, although your singleTop Activity has been completed or the entire program has been completed.
The current solution is:Mark and intercept in the Activity set as singleTop.
Android keypad blocks the interface
Set in AndroidManifest. xml
android:windowSoftInputMode="stateUnspecified|adjustPan"
Do not squeeze the screen, and vice versa.
<activity android:name=".activity.MainTabActivity" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" android:launchMode="singleTask" android:windowSoftInputMode="stateUnspecified|adjustPan"></activity>
NOTE: If an Activity in TabActivity has EditText and a soft keyboard pop-up, the Activity setting does not work and needs to be set in TabActivity.
Android uses AsyncTask to download images. It is best to use WeakReference
Some people like to use AsyncTask provided by Android, but in fact AsyncTask is more serious. This memory leakage problem occurs only when the run function does not end, however, the internal implementation mechanism of AsyncTask is to use ThreadPoolExcutor. the life cycle of the Thread object generated by this class is uncertain and cannot be controlled by the application. Therefore, if AsyncTask is used as the internal class of the Activity, it is more prone to memory leakage.
How should we solve the memory leakage caused by such threads?
First, change the internal class of the thread to the static internal class.
Second, use weak references within the thread to save Context references.
The solution model is as follows:
public abstract class WeakAsyncTask<Params, Progress, Result, WeakTarget> extends AsyncTask<Params, Progress, Result> { protected WeakReference<WeakTarget> mTarget; public WeakAsyncTask(WeakTarget target) { mTarget = new WeakReference<WeakTarget>(target); } /** {@inheritDoc} */ @Override protected final void onPreExecute() { final WeakTarget target = mTarget.get(); if (target != null) { this.onPreExecute(target); } } /** {@inheritDoc} */ @Override protected final Result doInBackground(Params... params) { final WeakTarget target = mTarget.get(); if (target != null) { return this.doInBackground(target, params); } else { return null; } } /** {@inheritDoc} */ @Override protected final void onPostExecute(Result result) { final WeakTarget target = mTarget.get(); if (target != null) { this.onPostExecute(target, result); } } protected void onPreExecute(WeakTarget target) { // No default action } protected abstract Result doInBackground(WeakTarget target, Params... params); protected void onPostExecute(WeakTarget target, Result result) { // No default action } }
Android webview
WebView. loadUrl (video_url); webView. setInitialScale (50); WebSettings setting = webView. getSettings (); setting. setUseWideViewPort (true); setting. setJavaScriptEnabled (true); // Support JavaScript setting. setPluginsEnabled (true); // Support Plugins, for example just like flash plugin. setting. setSupportZoom (true); // Zoom Control on web (You don't need this if ROM supports Multi-Touch setting. setBuiltInZoom Controls (true); // Enable Multitouch if supported by ROM webView. setWebViewClient (new MyWebViewClient (); public class MyWebViewClient extends WebViewClient {private ProgressDialog loadingBar; @ Overridepublic void Preview (WebView view, String url, Bitmap favicon) {loadingBar = progressdidialog. show (mActivity, null, "loading... "); Super. onPageStarted (view, url, favicon) ;}@ Overridepublic boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); // The Link in webview also loads return true in the same page;} @ Overridepublic void onPageFinished (WebView view, String url) {if (loadingBar. isShowing () {loadingBar. dismiss ();} super. onPageFinished (view, url) ;}@ Overridepublic void onReceivedError (WebView view, int errorCode, Stri Ng description, String failingUrl) {Toast. makeText (mActivity, "loading error! ", Toast. LENGTH_LONG ). show (); final AlertDialog alertDialog = new AlertDialog. builder (mActivity ). create (); alertDialog. setTitle ("ERROR"); alertDialog. setMessage (description); alertDialog. setButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {alertDialog. dismiss () ;}}); alertDialog. show ();}}
Network download policy and File Cache Policy in Android Development
The general cache policy is:
Level-1 memory cache, level-2 File Cache (the database is also counted as a File Cache), and level-3 network data
I. Cache Policy for Online downloads
Basic Strategies for downloading files (images, audios, and videos) over the Internet:
1. Do not directly download the file to the target file. Use the temp file for transit to ensure the correctness and integrity of the file. The process is as follows:
A) Generate A unique local target file name B with the network target file name.
B) generate a unique local temporary file name with the local target file name B. T
C) download the file to T
D. Check the correctness and integrity of file T after the download is completed.
E) if it is incorrect or incomplete, delete the file T and return false.
F) after the verification is completed, rename or copy the file T to file B.
G) The final cleaning site, deleting the temporary file T, returns true after successful
2. Verify file correctness and integrity as much as possible:
A) Correctness: for example, MD5/Hash Code comparison and file format comparison.
B) Integrity: for example, whether the file size is consistent and the image data is correct (related information is provided in the image file header)
3. Consider whether secondary processing is required for downloading a local file. Consider the following:
A) For example, if the size of the image starting from the network source is 800*600, and the size of the thumbnail is 160*145, You need to crop the downloaded file and save it, delete the source file directly.
Ii. File Cache Policy:
1. The corresponding I/O key of the unique cache file is required. Generally, hashcode can be used.
2. If the same file takes different time, consider clearing the local cache first and then downloading the new cache to the local.
3. You can add a timestamp to a file to generate a unique hashcode.
4. During file generation, you may need to take the following comprehensive considerations:
A) is there no space for sdcard (this requirement exists, but almost no one will consider it. Once it happens, it will be crash ).
B) cache cleaning policy. Daily and weekly cleaning? After a threshold value is reached, will it be automatically cleared? (If there is no cleanup policy, it is very SB to keep junk data as a treasure ).
C) cache the actually needed data. Do not think that external storage is infinite, so you can store everything. You need to know that many things are complex and complex. A colleague once stored several hundred MB of user data (gender, age, and contact information of all users) every day, while PM only needed an active data report for several users every day, therefore, the daily user analysis report data can be cached (only 10 KB ).
D) encrypt the cache file. The simplest is to remove the file extension, which is also encrypted. Of course, you can encrypt the Server File and then decrypt it in the memory. This depends on the needs of the project, and my experience is insufficient. It is generally used to change the extension.
Iii. Memory Cache Policy
The memory cache policy is designed to cope with more efficient caching. For file data that is not frequently updated, it can be cached in the memory, but there are the following considerations:
A) memory I/O, weak reference. Weak references can make JVM more reasonable and self-collect.
B) the memory cache key must also be unique. For details, refer to the File Cache key policy.
C) provides an interface for refreshing the memory cache. Although the lifecycle of the memory cache itself is short, it must be the top-level user data. Therefore, it can be considered to provide a UI refresh interface during design.
The addFooterView of ListView in Android is not displayed.
mListView.addFooterView(btn_more, null, false);mListView.setAdapter(mBlogListAdapter);
Put addFootView before setAdapter.
Android automatically pops up the keyboard
Add:
// Automatically call up the soft keyboard Timer timer = new Timer (); timer. schedule (new TimerTask () {@ Override public void run () {(InputMethodManager) getSystemService (INPUT_METHOD_SERVICE )). toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS); }}, 200 );
I am the dividing line of tiantiao
What is android
Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.
What is android
Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.