首页 > 代码库 > Handler

Handler

Handler的使用

常用方法

post(Runnable runnable)  启动线程

removeCallbacks(Runnable runnable)  取消线程

postDelayed(Runnable runnable,int time)  延时启动线程

 

例、应用启动的欢迎界面

XML

首先是welcome.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     xmlns:android="http://schemas.android.com/apk/res/android"> 6  7     <ImageView 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:id="@+id/imageView"11         android:layout_alignParentLeft="true"12         android:layout_alignParentStart="true"13         android:layout_alignParentTop="true"14         android:layout_alignParentBottom="true"15         android:src="@drawable/music"16         android:layout_alignParentRight="true"17         android:layout_alignParentEnd="true" />18 19 </RelativeLayout>

然后是main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingLeft="@dimen/activity_horizontal_margin" 6     android:paddingRight="@dimen/activity_horizontal_margin" 7     android:paddingTop="@dimen/activity_vertical_margin" 8     android:paddingBottom="@dimen/activity_vertical_margin" 9     tools:context=".MyActivity">10 11     <TextView12         android:text="@string/hello_world"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content" />15 16 </RelativeLayout>

JAVA

welcome.java

import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.view.Window;import android.widget.ImageView;public class welcome extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.welcome);        new  Handler().postDelayed(new Runnable() {            @Override            public void run() {                Intent mainIntent = new Intent(welcome.this,MyActivity.class);                welcome.this.startActivity(mainIntent);                welcome.this.finish();            }        },3000);    }}

  这里使用requestWindowFeature(Window.FEATURE_NO_TITLE)将标题栏隐藏。创建一个子线程,延时3秒跳转的下一个Activity。

 

MyActivity.java

import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MyActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_my);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.my, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}