如题所述
ãä¸ç¥éä½ æ£å¨è¯å¾åã æ¨å¯ä»¥ä½¿ç¨ä¸é¢ï¼ä»¥ä¾åèã
ããå¦æä½ å®ç°Thread or HandlerThread ï¼ç¡®ä¿æ¨çUI线ç¨ä¸ä¼é»å¡èçå¾ å·¥ä½çº¿ç¨å®æä¸è´çµThread.wait()æThread.sleep()
ããæ£æ¥developer.android.com/training/articles/perf-anr.html
ããä½ ä¸åºè¯¥é»æ¢ä½ ç主UI线ç¨ã èä¸æ¯ä¸ä¸ªå®æ¶å¨ï¼ä½ å¯ä»¥ä½¿ç¨ä¸ä¸ª'å¤çå¨'
ããæ¨å¯ä»¥å¯å¨ååæ¢å®æ¶å¨æé®ç¹å»ã æ¨å¯ä»¥éæ°å®ææ¨çå®æ¶å¨è®¡æ°å¼ã ä½ éè¦ç §é¡¾çæ¹åååçæ´»å¨è¢«éæ¯å¹¶éæ°å建ã countçå¼ï¼æ们å°éæ°åå§åã
ããMainActivity.java
ããpublic class MainActivity extends Activity implements OnClickListener{
ããTimer t;
ããint count = 0;
ããboolean isDone = true;
ããButton b;
ããTextView tv;
ããThread thread;
ãã@Override
ããprotected void onCreate(Bundle savedInstanceState) {
ããsuper.onCreate(savedInstanceState);
ããsetContentView(R.layout.main);
ããb = (Button) findViewById(R.id.button1);
ããtv = (TextView) findViewById(R.id.textView1);
ããt = new Timer();
ããt.scheduleAtFixedRate(new TimerTask() {
ãã@Override
ããpublic void run() {
ãã// TODO Auto-generated method stub
ããrunOnUiThread(new Runnable() {
ããpublic void run() {
ããtv.setText(String.valueOf(count));
ããif(isDone)
ããcount++;
ãã}
ãã});
ãã}
ãã}, 1000, 1000);
ããb.setOnClickListener(this);
ãã}
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stu
ããfinal Dialog d = new Dialog(MainActivity.this);
ããd.setTitle("Pause Menu");
ããd.setContentView(R.layout.pausemenu);
ããButton b1 = (Button) d.findViewById(R.id.button1);
ããButton b2 = (Button) d.findViewById(R.id.button2);
ããb1.setOnClickListener(new OnClickListener() {
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stub
ããt.cancel();
ããd.dismiss();
ãã}
ãã});
ããb2.setOnClickListener(new OnClickListener() {
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stub
ããif(isDone)
ãã{
ããt.cancel();
ããisDone=false;
ãã}
ããt = new Timer();
ããt.scheduleAtFixedRate(new TimerTask() {
ãã@Override
ããpublic void run() {
ãã// TODO Auto-generated method stub
ããrunOnUiThread(new Runnable() {
ããpublic void run() {
ããtv.setText(String.valueOf(count));
ããcount++;
ãã}
ãã});
ãã}
ãã}, 1000, 1000);
ããd.dismiss();
ãã}
ãã});
ããd.show();
ãã}
ãã}
ããmain.xml
ãã<?xml version="1.0" encoding="utf-8"?>
ãã<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
ããandroid:layout_width="match_parent"
ããandroid:layout_height="match_parent" >
ãã<TextView
ããandroid:id="@+id/textView1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_centerHorizontal="true"
ããandroid:layout_centerVertical="true"
ããandroid:textSize="20sp"
ããandroid:text="TextView" />
ãã<Button
ããandroid:id="@+id/button1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignParentBottom="true"
ããandroid:layout_alignRight="@+id/textView1"
ããandroid:layout_marginBottom="21dp"
ããandroid:text="Button" />
ãã</RelativeLayout>
ããpausemenu.xml
ãã<?xml version="1.0" encoding="utf-8"?>
ãã<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
ããandroid:layout_width="match_parent"
ããandroid:layout_height="match_parent" >
ãã<Button
ããandroid:id="@+id/button1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignParentBottom="true"
ããandroid:layout_alignParentLeft="true"
ããandroid:layout_marginBottom="118dp"
ããandroid:layout_marginLeft="51dp"
ããandroid:text="Stop" />
ãã<Button
ããandroid:id="@+id/button2"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignBaseline="@+id/button1"
ããandroid:layout_alignBottom="@+id/button1"
ããandroid:layout_marginLeft="32dp"
ããandroid:layout_toRightOf="@+id/button1"
ããandroid:text="Start" />
ãã</RelativeLayout>
ããç¼è¾ï¼
ãã使ç¨å¤çç¨åº
ããHandler m_handler;
ããRunnable m_handlerTask ;
ããm_handlerTask = new Runnable()
ãã{
ãã@Override
ããpublic void run() {
ããif(isDone)
ãã{
ããtv.setText(""+count);
ããcount++;
ããm_handler.postDelayed(m_handlerTask, 1000);
ãã}
ãã// m_handler.removeCallbacks(m_handlerTask);
ãã}
ãã};
ããm_handlerTask.run();
ããä½ åºè¯¥åæ¢å®æ¶å¨åå¤çç¨åºæ¶ï¼ä¸éè¦åæ¶ã
ããå¦æä½ t.cancel()使ç¨å®æ¶å¨å使ç¨m_handler.removeCallbacks(m_handlerTask)åæ¢å¤çç¨åºå¼å«
ããå¦æä½ å®ç°Thread or HandlerThread ï¼ç¡®ä¿æ¨çUI线ç¨ä¸ä¼é»å¡èçå¾ å·¥ä½çº¿ç¨å®æä¸è´çµThread.wait()æThread.sleep()
ããæ£æ¥developer.android.com/training/articles/perf-anr.html
ããä½ ä¸åºè¯¥é»æ¢ä½ ç主UI线ç¨ã èä¸æ¯ä¸ä¸ªå®æ¶å¨ï¼ä½ å¯ä»¥ä½¿ç¨ä¸ä¸ª'å¤çå¨'
ããæ¨å¯ä»¥å¯å¨ååæ¢å®æ¶å¨æé®ç¹å»ã æ¨å¯ä»¥éæ°å®ææ¨çå®æ¶å¨è®¡æ°å¼ã ä½ éè¦ç §é¡¾çæ¹åååçæ´»å¨è¢«éæ¯å¹¶éæ°å建ã countçå¼ï¼æ们å°éæ°åå§åã
ããMainActivity.java
ããpublic class MainActivity extends Activity implements OnClickListener{
ããTimer t;
ããint count = 0;
ããboolean isDone = true;
ããButton b;
ããTextView tv;
ããThread thread;
ãã@Override
ããprotected void onCreate(Bundle savedInstanceState) {
ããsuper.onCreate(savedInstanceState);
ããsetContentView(R.layout.main);
ããb = (Button) findViewById(R.id.button1);
ããtv = (TextView) findViewById(R.id.textView1);
ããt = new Timer();
ããt.scheduleAtFixedRate(new TimerTask() {
ãã@Override
ããpublic void run() {
ãã// TODO Auto-generated method stub
ããrunOnUiThread(new Runnable() {
ããpublic void run() {
ããtv.setText(String.valueOf(count));
ããif(isDone)
ããcount++;
ãã}
ãã});
ãã}
ãã}, 1000, 1000);
ããb.setOnClickListener(this);
ãã}
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stu
ããfinal Dialog d = new Dialog(MainActivity.this);
ããd.setTitle("Pause Menu");
ããd.setContentView(R.layout.pausemenu);
ããButton b1 = (Button) d.findViewById(R.id.button1);
ããButton b2 = (Button) d.findViewById(R.id.button2);
ããb1.setOnClickListener(new OnClickListener() {
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stub
ããt.cancel();
ããd.dismiss();
ãã}
ãã});
ããb2.setOnClickListener(new OnClickListener() {
ãã@Override
ããpublic void onClick(View v) {
ãã// TODO Auto-generated method stub
ããif(isDone)
ãã{
ããt.cancel();
ããisDone=false;
ãã}
ããt = new Timer();
ããt.scheduleAtFixedRate(new TimerTask() {
ãã@Override
ããpublic void run() {
ãã// TODO Auto-generated method stub
ããrunOnUiThread(new Runnable() {
ããpublic void run() {
ããtv.setText(String.valueOf(count));
ããcount++;
ãã}
ãã});
ãã}
ãã}, 1000, 1000);
ããd.dismiss();
ãã}
ãã});
ããd.show();
ãã}
ãã}
ããmain.xml
ãã<?xml version="1.0" encoding="utf-8"?>
ãã<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
ããandroid:layout_width="match_parent"
ããandroid:layout_height="match_parent" >
ãã<TextView
ããandroid:id="@+id/textView1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_centerHorizontal="true"
ããandroid:layout_centerVertical="true"
ããandroid:textSize="20sp"
ããandroid:text="TextView" />
ãã<Button
ããandroid:id="@+id/button1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignParentBottom="true"
ããandroid:layout_alignRight="@+id/textView1"
ããandroid:layout_marginBottom="21dp"
ããandroid:text="Button" />
ãã</RelativeLayout>
ããpausemenu.xml
ãã<?xml version="1.0" encoding="utf-8"?>
ãã<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
ããandroid:layout_width="match_parent"
ããandroid:layout_height="match_parent" >
ãã<Button
ããandroid:id="@+id/button1"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignParentBottom="true"
ããandroid:layout_alignParentLeft="true"
ããandroid:layout_marginBottom="118dp"
ããandroid:layout_marginLeft="51dp"
ããandroid:text="Stop" />
ãã<Button
ããandroid:id="@+id/button2"
ããandroid:layout_width="wrap_content"
ããandroid:layout_height="wrap_content"
ããandroid:layout_alignBaseline="@+id/button1"
ããandroid:layout_alignBottom="@+id/button1"
ããandroid:layout_marginLeft="32dp"
ããandroid:layout_toRightOf="@+id/button1"
ããandroid:text="Start" />
ãã</RelativeLayout>
ããç¼è¾ï¼
ãã使ç¨å¤çç¨åº
ããHandler m_handler;
ããRunnable m_handlerTask ;
ããm_handlerTask = new Runnable()
ãã{
ãã@Override
ããpublic void run() {
ããif(isDone)
ãã{
ããtv.setText(""+count);
ããcount++;
ããm_handler.postDelayed(m_handlerTask, 1000);
ãã}
ãã// m_handler.removeCallbacks(m_handlerTask);
ãã}
ãã};
ããm_handlerTask.run();
ããä½ åºè¯¥åæ¢å®æ¶å¨åå¤çç¨åºæ¶ï¼ä¸éè¦åæ¶ã
ããå¦æä½ t.cancel()使ç¨å®æ¶å¨å使ç¨m_handler.removeCallbacks(m_handlerTask)åæ¢å¤çç¨åºå¼å«
温馨提示:答案为网友推荐,仅供参考