定义BaseApplication并配置Application
import android.app.Application;
import android.os.Handler;
/**
*
* ============================================================
**/
public class BaseApplication extends Application {
//获取到主线程的上下文
private static BaseApplication mContext;
//获取到主线程的handler
private static Handler mMainThreadHanler;
//获取到主线程
private static Thread mMainThread;
//获取到主线程的id
private static int mMainThreadId; @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.mContext = this;
this.mMainThreadHanler = new Handler();
this.mMainThread = Thread.currentThread();
//获取到调用线程的id
this.mMainThreadId = android.os.Process.myTid();
} public static BaseApplication getApplication(){
return mContext;
} public static Handler getMainThreadHandler(){
return mMainThreadHanler;
} public static Thread getMainThread(){
return mMainThread;
} public static int getMainThreadId(){
return mMainThreadId;
} }
BaseApplication
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heima.googleplay"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application
android:name="包名.application.BaseApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.heima.googleplay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
manifest
<application
android:name="包名.application.BaseApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.heima.googleplay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>