PackageOrg.tonny;ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.widget.TextView; Public classMainactivityextendsactivity{PrivateTextView mTv; Private Final intTime = 1000; PrivateTimer Mtimer; PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { Case1: //The Asynctask was created here, not in the TimerTask run . Newoperasynctask (). Execute (Mtv.gettext (). toString ()); Break; } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); MTv=(TextView) Findviewbyid (r.id.tv_content); //the TimerTask + handler method is used to achieveMtimer =NewTimer (); Mtimer.schedule (NewTimerTask () {@Override Public voidrun () {//cannot create operasynctask directly here, will error//so that's how it's used.Message message =Mhandler.obtainmessage (); Message.what= 1; Mhandler.sendmessage (message); }}, time, time); //execute task after 1s with a period of 1s } /*** String Processing * *@paramcontent *@return */ Privatestring Moveheader2tailbyone (string content) {//gets the first elementString Header = content.substring (0, 1); //gets all the elements that follow from the start of the second elementString tail = content.substring (1); returnTail +header; } /*** Define threads for processing and updating of strings * *@authorAdministrator **/ Private Final classOperasynctaskextendsAsynctask<string, Void, string>{@Overrideprotectedstring Doinbackground (String ... params) {returnMoveheader2tailbyone (params[0]); } @Overrideprotected voidOnPostExecute (String result) {Mtv.settext (result); } }}
There is a problem with string handling, why can't I create an asynchronous thread in a task?
Android Learning 12: Marquee program Implementation (simple contact)