Creates a new class MyApplication, inherited from application. The code is as follows:
Myapplication.java:
1 PackageCom.smyhvae.homepicdemo;2 3 Importandroid.app.Application;4 ImportAndroid.os.Handler;5 ImportAndroid.os.Looper;6 7 /**8 * Created by smyhvae on 2015/5/13.9 */Ten Public classMyApplicationextendsApplication { One //gets the context to the main thread A Private StaticMyApplication Mcontext =NULL; - //gets the handler to the main thread - Private StaticHandler Mmainthreadhandler =NULL; the //gets the looper to the main thread - Private StaticLooper Mmainthreadlooper =NULL; - //get to the main thread - Private StaticThread Mmainthead =NULL; + //gets the ID to the main thread - Private Static intMmaintheadid; + A @Override at Public voidonCreate () { - //TODO auto-generated Method Stub - Super. OnCreate (); - This. Mcontext = This; - This. Mmainthreadhandler =NewHandler (); - This. Mmainthreadlooper =Getmainlooper (); in This. Mmainthead =Thread.CurrentThread (); - //Android.os.Process.myUid () Gets the user ID to //Android.os.Process.myPid () Gets the process ID + //Android.os.Process.myTid () Gets the ID of the calling thread - This. Mmaintheadid =Android.os.Process.myTid (); the } * $ Public StaticMyApplication getapplication () {Panax Notoginseng returnMcontext; - } the + Public StaticHandler Getmainthreadhandler () { A returnMmainthreadhandler; the } + - Public StaticLooper Getmainthreadlooper () { $ returnMmainthreadlooper; $ } - - Public StaticThread Getmainthread () { the returnMmainthead; - }Wuyi the Public Static intGetmainthreadid () { - returnMmaintheadid; Wu } - About}
Then remember to declare it in the manifest file:
1 < application 2 = "true" 3 Android:icon = "@mipmap/ic_launcher" 4 Android: Label = "@string/app_name" 5 Span style= "color: #ff0000;" > Android:theme = "@style/apptheme" 6
Android:name =". MyApplication " >
The property that needs to be declared is the 6th line of Android:name in the code above.
Android code optimization----template notation for application nodes