Android-compatible Java Engineering 1.0

Source: Internet
Author: User

Android-compatible systems
Package Cn.com.likeshow;import Java.lang.reflect.constructor;import java.lang.reflect.InvocationTargetException; Import myvm.test.myactivity;/* * Author Zhang Zhang * * Time 2014-5-12 * */public class Activity {private Bundle Bundle;//savedi Nstancestate for activity See Transfer parameters protected void OnCreate (Bundle savedinstancestate) {this.bundle=savedinstancestate;} Protected Intent getintent () {return bundle.getintent ();} protected static Context Getapplicationcontext () {return null;} protected void StartActivity (Intent Intent) {//Create activity Activity=intent.getpreactivity () by reflection completion object; Class class1=intent.getafteractivity (); Constructor con=class1.getconstructors () [0]; try {activity activity2= (activity) con.newinstance ();//pass Bundele most parameters, call Getintent () The parameter activity2.oncreate (new bundle (intent)) passed between the activity is obtained from the Bundle. catch (Exception e) {e.printstacktrace ();}} protected void Setcontentview (int id) {System.out.println ("Setcontentview" + ID);} Protected Boolean Oncreateoptionsmenu (Menu menu) {return true;} ProtectedBoolean onoptionsitemselected (MenuItem Item) {return true;} Protected static class Toast{private Context applicationcontext;private static String s;private int t;private Toast (Conte XT ApplicationContext2, string string, int i) {this.applicationcontext=applicationcontext2;this.s=string;this.t=i;} public static Toast Maketext (Context applicationcontext,string String, int i) {//actual Android does not necessarily return toast object, this case is only used to simulate the Android implementation principle RET   Urn New Toast (applicationcontext,string,i);   } public static void show () {int len=s.length () +2; for (int i=0;i<len;i++) System.out.print ("*");  System.out.print ("\n*" +s+ "*\n"); for (int i=0;i<len;i++) System.out.print ("*");   System.out.println (); }}public static void Main (string[] args) {//For convenience, assuming the default startup MyActivity, of course, you can read the configuration file to know the default boot Activitynew myactivity (). OnCreate (new Bundle (null));}}


Package Cn.com.likeshow;public class Bundle {Private  Intent intent;public bundle (Intent Intent) {this.intent= Intent;} Public Intent getintent () {return Intent;} public void Setintent (Intent Intent) {this.intent = Intent;}}


Package Cn.com.likeshow;import Java.io.objectinputstream.getfield;import Java.util.hashmap;import java.util.Map; public class Intent {private map<string, object>map;private Activity activity;private class Class1;public Intent ( Activity activity, CLASS< Extends activity> Class1) {map=new hashmap<string, object> (); this.activity=activity;this.class1=class1;} public void PutExtra (String str,object obj) {map.put (str, obj);} Public Object Getextra (String str) {return  map.get (str);} Public activity getpreactivity () {return activity;} Public Class getafteractivity () {return class1;}}


========== Simple example Android code is as follows =========================================================

Package Myvm.test;import Cn.com.likeshow.activity;import Cn.com.likeshow.bundle;import cn.com.likeshow.Intent; public class MyActivity extends Activity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); System.out.println ("myactivity start"); Setcontentview (1); Toast.maketext (Getapplicationcontext (), "haha toast", 0). Show (); Intent intent=new Intent (Myactivity.this, Secondactivity.class); Intent.putextra ("MyActivity", "Hello 2activity"); StartActivity (intent); Toast.maketext (Getapplicationcontext (), "secondactivity start Complete", 0). Show (); Intent intent2=new Intent (Myactivity.this, Thirdactivity.class); Intent2.putextra ("1", "Hello 3activity"); StartActivity (Intent2); Toast.maketext (Getapplicationcontext (), "thirdactivity start Complete", 0). Show ();}}


Package Myvm.test;import Cn.com.likeshow.activity;import Cn.com.likeshow.bundle;import cn.com.likeshow.Intent; public class Secondactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); System.out.println ("secondactivity start"); Setcontentview (2); Intent intent=getintent (); Object Object=intent.getextra ("myactivity"); Object Object2=intent.getextra ("1"); System.out.println ("secondactivity" +object); System.out.println ("secondactivity" +object2); Toast.maketext (Getapplicationcontext (), "Secondactivity execution Complete", 0). Show ();}}


Package Myvm.test;import Cn.com.likeshow.activity;import Cn.com.likeshow.bundle;import cn.com.likeshow.Intent; public class Thirdactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); System.out.println ("thirdactivity start"); Setcontentview (3); Intent intent=getintent (); Object Object=intent.getextra ( "MyActivity"); Object Object2=intent.getextra ("1"); System.out.println ("thirdactivity" +object); System.out.println ("thirdactivity" +object2); Toast.maketext (Getapplicationcontext (), "Thirdactivity execution Complete", 0). Show ();}}


========== =================

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.