码迷,mamicode.com
首页 > 移动开发 > 详细

兼容安卓的java工程1.0

时间:2014-05-13 11:24:32      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:安卓   系统   兼容   java   虚拟机   

 
//兼容安卓的系统
package cn.com.likeshow;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

 
import myvm.test.myActivity;
/*
 * author  张章
 * 
 * time 2014-5-12
 * 
 */
public class Activity {

	private Bundle bundle;
	
	//savedInstanceState用于activity见传递参数
	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) {
		//通过反射完成对象创建
		Activity activity=intent.getPreActivity();
		Class class1=intent.getAfterActivity();
		
		 Constructor con=class1.getConstructors()[0];
		 try {
			Activity activity2=(Activity) con.newInstance();
			//把Bundele最为参数传入,调用getIntent()时会从bundle中获取activity间传递的参数
			activity2.onCreate(new Bundle(intent));
			
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		 
	}

	protected void setContentView(int id) {
		System.out.println("setContentView " + id);
	}

	protected boolean onCreateOptionsMenu(Menu menu) {
		 
		return true;
	}

	protected boolean onOptionsItemSelected(MenuItem item) { 
		return true;
	}
	
	
	protected static class Toast
	{

		private Context applicationContext;
		private static String s;
		private int t;

		private Toast(Context applicationContext2, String string, int i) {
			this.applicationContext=applicationContext2;
			this.s=string;
			this.t=i;
		}
		public static Toast makeText(Context applicationContext,String string, int i) 
	   { 
			 //实际安卓中不一定是返回Toast对象,该事例只用于模拟安卓实现原理
			return 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) {
		//方便起见,假定默认启动myActivity,当然可以通过读取配置文件,知道默认启动的Activity
		new 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;
		
	}
	
}


//==========简易示例安卓代码如下=========================================================

package myvm.test;

import cn.com.likeshow.Activity;
import cn.com.likeshow.Bundle;
import cn.com.likeshow.Intent;

public class myActivity extends Activity {
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("myActivity 启动");
		setContentView(1);
		Toast.makeText(getApplicationContext(),"哈哈Toast",0).show();
		
		Intent intent=new Intent(myActivity.this, SecondActivity.class);
		intent.putExtra("myActivity", "hello 2activity");
		
		startActivity(intent);
		
		Toast.makeText(getApplicationContext(),"SecondActivity启动完毕",0).show();
		
		Intent intent2=new Intent(myActivity.this, ThirdActivity.class);
		intent2.putExtra("1", "hello 3activity");
		
		startActivity(intent2);
		Toast.makeText(getApplicationContext(),"ThirdActivity启动完毕",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{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("SecondActivity 启动");
		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执行完毕",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 {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("ThirdActivity 启动");
		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执行完毕",0).show();
	}
}


==========效果图 =================

bubuko.com,布布扣

兼容安卓的java工程1.0,布布扣,bubuko.com

兼容安卓的java工程1.0

标签:安卓   系统   兼容   java   虚拟机   

原文地址:http://blog.csdn.net/qingchunweiliang/article/details/25658827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!