Home Unity3D Channel [Institute of Unity3D game development hello Unity3D] [] [] [NGUI essays Unity Unity plugin Institute of channels [game] Android Android Institute of Development Research] [the] application development Android IOS Channel [Cocos2D Game Development Institute of IOS application development] [] [Institute of Institute of Objective-C syntax] [application development] Three20 Institutes of my work contact webmaster if the program's main thread is blocked more than five seconds, the system prednisone will prompt "Application not responding" That's ANR. ANR stands for Application Not Responding, using multiple threads to avoid ANR. But not here to take note of in order to avoid excessive use of ANR and multi-threading, the situation unless a last resort. For example, to access the network server returned too slow, too much data leads to slide the screen is not smooth, or I / O read too much resources and so on. Here you can start a new thread to handle these time-consuming operation. If the excessive use of multithreading data synchronization problems occur programmer needs to deal with, so when using multiple prednisone threads try to keep its independence will not be other threads intervention. java language provides a thread-locking concept synchronized method can add the object lock and lock specifically avoid multiple threads simultaneously access problems lead to a method or an object, interested prednisone friends can go and see it here, I do not wordy.
import java.io.InputStream; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; prednisone import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class SingleActivity extends Activity {/ ** read ** progress / public final static prednisone int LOAD_PROGRESS = 0; / ** mark the end of reading progress ** / public final static int LOAD_COMPLETE = 1; / ** button to start loading 100 images ** / Button mButton = null; / ** display ** / TextView mTextView = null; Time / ** load images before ** / Long mLoadStatr = 0L; Time / ** Load picture after ** / Long mLoadEnd = 0L; Context mContext = null; / / Receive passed over Information Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg.what) {case LOAD_PROGRESS: mTextView.setText ("current reading to the first" + msg.arg1 + "pictures" ); break; case LOAD_COMPLETE: mTextView.setText ("read the end of the total time-consuming" + msg.arg1 + "ms"); break;} super.handleMessage (msg);}}; @ Override protected void onCreate (Bundle savedInstanceState ) {setContentView (R.layout.single); mContext = this; / ** get the button with the TextView object ** / mButton = (Button) findViewById (R.id.button0); mTextView = (TextView) findViewById prednisone (R. id.textView0); mTextView.setText ("Click the button to start the update time"); mButton.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {/ / start reading the image LoadImage ();}}) ; super.onCreate (savedInstanceState);} public void LoadImage () {new Thread () {@ Override public void run () {/ / get the start time mLoadStatr load images = System.currentTimeMillis (); for (int i = 0 ; i <100; i + +) {/ / here cyclic loading prednisone pictures 100 times ReadBitMap (mContext, R.drawable.bg); / / Each finished reading a picture to schedule training and preparation handler Message msg = new Message (); msg . what = LOAD_PROGRESS; msg.arg1 = i + 1; handler.sendMessage (msg);} / / get the time to load images mLoadEnd end = System.currentTimeMillis (); / / 100 images finished loading Message msg = new Message ( ); msg.what = LOAD_COMPLETE; msg.arg1 = (int) (mLoadEnd - mLoadStatr);. handler.sendMessage (msg);}} start ();} / *** read local resources pictures prednisone ** @ param context * @ param resId * @ return * / public Bitmap ReadBitMap (Context context, int resId) {BitmapFactory.Options opt = new BitmapFactory.Options (); opt.inPreferredConfig prednisone = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt . inInputShareable = true; / / get the resource picture InputStream prednisone is = context.getResources () openRawResource (resId);. return BitmapFactory.decodeStream (is, null, opt);}}
16
32
48
64
80
96
112
import android view View OnClickListener...;
/ ** Load images before the time ** /
. mTextView setText ("read the end of the total time-consuming" + msg arg1 + "milliseconds.");
mButton. setOnClickListener (new OnClickListener () {
. mLoadStatr = System currentTimeMillis ();
/ / 100 image is loaded
* /
Timer and TimerTask can build an access time delay like to open a thread than the specified time intervals. prednisone You can go to close the Timer and TimerTask prednisone in this thread, for example for example, now I have to do an online account login timeout client detects the user enter a username and password click on Login and this time I open a TimerTask each had one second to check whether the user login success, after 10 seconds if it is not successful login prompt him landing overtime. This time I need to detect in this thread to close the Timer and TimerTask because it is not needed in the loop to detect. Call to cancel () can be turned off, ask someone to read the following example.
import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TimerTaskActivity extends Activity {/ ** execute Timer progress ** / public final static int LOAD_PROGRESS = 0; / ** Close Timer progress ** / public final static int CLOSE_PROGRESS = 1; / ** TimerTask start button ** / Button mButton0 = null; / ** Close TimerTask button ** / Button mButton1 = null; / ** display ** / TextView mTextView = null; Context mContext = null; / ** Timer object ** / Timer mTimer = null; / ** TimerTask object ** / TimerTask mTimerTask = null; / ** Record TimerID ** / int mTimerID = 0; / ** receives information transmitted over ** / Handler handler = new Handler () {@ Override public void handleMessage (Message prednisone msg) {switch (msg.what) {case LOAD_PROGRESS: mTextView.setText ("Current TimerID as "+ msg.arg1); break; case CLOSE_PROGRESS: mTextView.setText (" Current Timer has been closed, please re-open "); break;} super.handleMessage (msg);}}; @ Override protected void onCreate (Bundle savedInstanceState) {setContentView (R.layout.timer); mContext = this; / ** get the button with the TextView object ** / mButton0 = (Button) prednisone f
No comments:
Post a Comment