Android Learning 12: Marquee program Implementation (simple Contact)

Source: Internet
Author: User

 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.