Android 四大组件之(1)Activity与Intent通信机制

1、使用Intent对象来指定一个Activity,并通过startActivity或startActivityForResult方法启动该Activity.以及在Activity之间传递数据:

public class MainActivity extends Activity {
	
	private Button btn1,btn2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout view = new LinearLayout(this);
        view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        btn1 = new Button(this);
        btn1.setId(1);
        btn1.setText("使用类名开启另一个Activity");
        btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        btn2 = new Button(this);
        btn2.setId(2);
        btn2.setText("使用action开启另一个Activity");
        btn2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        view.addView(btn1);
        view.addView(btn2);
        setContentView(view);
        
        btn1.setOnClickListener(listener);
        btn2.setOnClickListener(listener);
    }
    
    private View.OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			Intent intent = new Intent();
			switch (view.getId()) {
			case 1:
				intent.setClass(MainActivity.this, ResultActivity.class);
				intent.putExtra("name", "JAVA");
				startActivityForResult(intent, 222);
				break;
			case 2:
				intent.setAction("forResult");
				MyData myData = new MyData();
                                //使用putExtra传递自定义对象时,该对象必须实现序列化,该类中的所有成员属性也必须实现序列化接口
                                intent.putExtra("data", myData);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};



	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//requestCode是请求码,即startActivityForResult(intent, 222)中的第二个参数
		//resultCode是结果码,即setResult(333)中设置的数字
		switch (requestCode) {
		case 222:
			if(resultCode == 333){
				Toast.makeText(this, "无结果返回...", Toast.LENGTH_LONG).show();
			} else if(resultCode == 444){
				Toast.makeText(this, data.getStringExtra("result"), Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}
	}
}
public class ResultActivity extends Activity {
	private Button btn1 , btn2;
	private TextView tv;
	private Intent intent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		intent = getIntent();
		
		LinearLayout view = new LinearLayout(this);
		view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		
		tv = new TextView(this);
		tv.setText("name is "+intent.getStringExtra("name"));
		tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		
		btn1 = new Button(this);
		btn1.setId(1);
		btn1.setText("Back1");
		btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		
		btn2 = new Button(this);
		btn2.setId(2);
		btn2.setText("Back2");
		btn2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		
		view.addView(tv);
		view.addView(btn1);
		view.addView(btn2);
		
		btn1.setOnClickListener(listener);
		btn2.setOnClickListener(listener);
		setContentView(view);
	}

	private View.OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case 1:
				setResult(333);
				break;
			case 2:
				intent.putExtra("result", "带结果返回");
				setResult(444, intent);
				break;
			default:
				break;
			}
			ResultActivity.this.finish();
		}
	};

}
public class Result2Activity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		MyData data = (MyData)getIntent().getSerializableExtra("data");
		setTitle(data.value);
	}
}
public class MyData implements Serializable {
	public String value = "this is test";
}

2、Intent指定Activity的方式

2.1 直接使用Class

    code

intent.setClass(MainActivity.this, ResultActivity.class);

    manifest

<activity android:name=".ResultActivity"></activity>

2.2 使用Action

   code :

intent.setAction("forResult");

manifest :

<activity android:name=".Result2Activity">
            <intent-filter>
                <action android:name="forResult"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

2.3 使用包名

code :

ComponentName cn = new ComponentName(MainActivity.this, "com.zj.activity.another.ActivityA");

  intent.setComponent(cn);

manifest:

<activity android:name=".another.ActivityA"></activity>

阅读更多
个人分类: Android
上一篇android matrix camera处理图片绕X轴Y轴翻转(类似3D效果)
下一篇Android 四大组件之(2)Service实现原理以及AIDL语言的使用详解
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭