Android Handler Message Pass parameters

Source: Internet
Author: User

Recently found message, send message can pass parameters, this idea is very good, so write an example, click on the screen, send a message to the activity, pass two parameters, and the activity destroyed!

The program opens the interface:

Click on the screen to destroy activity popup toast:

mainactivity:

Package com.cn.Android;      Import android.app.Activity;      Import Android.os.Bundle;      Import Android.os.Handler;      Import Android.os.Message;            Import Android.widget.Toast;  Public classMainactivity extends Activity {finalStatic intCanshu =1;  PublicHandler mhandler=NewHandler () { Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseCanshu:string str1 = Msg.getdata (). GetString ("Text1");//Accept msg passed over the parametersString str2 = Msg.getdata (). GetString ("Text2");//Accept msg passed over the parametersinitfinishmainactivity (str1, str2);  Break; Default:  Break;                    }              }             }; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Initmainactivity (); }                     Public voidinitmainactivity () {Setcontentview (NewMainactivityview ( This, This)); }                     Public voidinitfinishmainactivity (String str1, String str2) {Toast.maketext (mainactivity. This, str1+str2, Toast.length_long). Show ();          Finish (); }      }  

Mainactivityview:

Package com.cn.android;      Import Android.content.Context;      Import Android.graphics.Bitmap;      Import Android.graphics.BitmapFactory;      Import Android.graphics.Canvas;      Import Android.graphics.Rect;      Import Android.os.Bundle;      Import Android.os.Message;      Import android.view.MotionEvent;            Import Android.view.View;  Public classMainactivityview extends view{Privatemainactivity activity; Privatecontext Context; PrivateBitmap M_bitmap;  PublicMainactivityview (context context, mainactivity activity) {super (context);  This. Activity =activity;  This. Context =context;          Initbitmap (); }                     Public voidInitbitmap () {M_bitmap= Bitmapfactory.decoderesource ( This. Getresources (), r.drawable.bg); } @Override Public voidOnDraw (canvas canvas) {canvas.drawbitmap (M_bitmap,0,0,NULL); } @Override PublicBoolean ontouchevent (motioneventEvent) {              intx = (int)Event. GetX (); inty = (int)Event. GetY ();              Rect rect; Rect=NewRect (0,0, the,480); if(Rect.contains (x, y)) {Message msg=NewMessage (); Msg.what=Mainactivity.canshu; Bundle Bundle=NewBundle (); Bundle.putstring ("Text1","An example of Daming's message passing parameter! ");//storing data in bundlesBundle.putstring ("Text2","time:2011-09-05");//put data into bundlesMsg.setdata (bundle);//mes transmits data using bundlesActivity.mHandler.sendMessage (msg);//send a message with handler in activity            }                            returnSuper.ontouchevent (Event); }                          }  

Android Handler Message Pass parameters

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.