0
点赞
收藏
分享

微信扫一扫

Android Activity间的通信

爪哇驿站 2022-05-01 阅读 83

1 Bundle传值

例如:A调用并将数据传给B 
1、A中调用代码: 
Intent intent = new Intent(); 
intent.setClass(A.this, B.class); 
Bundle mBundle = new Bundle(); 
mBundle.putString("passData", "hello!B");//压入数据,passData是自定义的识别标志,还可以put添加多个Int、String等类型的数据
intent.putExtras(mBundle); 
startActivity(intent);
A.this.finish();

2、B中接受数据的代码:
Bundle bundle = getIntent().getExtras();  
String data = bundle.getString( "passData" );// data的值为hello!B

注:如果要传送的数据条目颇多的话,可以将识别标志抽取为一个个常量来引用。既避免出错,又方便维护。

2 利用startActivityForResult与onActivityResult方法

例如:A调用并传值给B,B得到A的值,还需在结束时返回一个值给A;
这里有两种较为普遍的方式:
1、	显示调用:即A是可知的,B也是可知的,A直接通过B的名字来调用它,方法如下:
<1>A中代码:
static final int A_REQUEST = 0;
//开始调用,没有finish()
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivityForResult(intent, A_REQUEST);
//回调函数,可以在eclipse中按快捷键Alt+Shift+S ,弹出菜单后再按V键,勾选Activity类别中所要覆盖的方法onActivityResult(), 便会自动生成
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if (requestCode == A_REQUEST) {
		//根据由B返回的不同结果,进而执行不同的操作,data是由B返回来的数据
      if (resultCode == RESULT_CANCELED)
           
      else if(resultCode == RESULT_OK) {
//由于<2>中B调用的setResult方法中第一个参数是RESULT_OK,因此A执行此处,并且可以获得B返回的数据,如下:
String getFromB = data.getExtras().getString("DataKey");//getFromB 的值为 :Hello A!
	
       }
 	}
}
<2>B中的代码:
//在onCreate方法中添加
Bundle bundle = new Bundle();
bundle.putString("DataKey", "Hello A!");
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();

隐式调用:即B的名字不必知道,被调用的Activity由系统来确定。比如,我们在手机中常会遇到上传微博头像的操作,在我们点击选择头像时,系统会自动给我们选择一个可以浏览图像文件的外部应用程序来供我们选择,如果有多个可以进行选择的应用,则会弹出一个列表供我们使用。选定一个图像文件后,外部应用便会返回被选文件的URI供微博应用程序使用。此即为隐式调用。

常用的隐式调用有:

<1>此为调用录音程序,返回录音文件的URI
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, 0);  
<2>此为调用拍照程序进行录像,返回视频文件的URI
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 0);  
<3>此为调用拍照程序进行拍照,返回照片的URI
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 0);  
<4>此为调用系统的图片剪裁程序,返回剪裁后的图像的URI
Intent intent = new Intent(Intent.ACTION_PICK, null);  
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
startActivityForResult(intent, 0);  
<5>浏览图像获取系统某个图像文件的方法,返回所选图片的URI
Intent localIntent1 = new Intent();
localIntent1.setType("image/*");
localIntent1.setAction("android.intent.action.GET_CONTENT");
Intent localIntent2 = Intent.createChooser(localIntent1,"选择图片:"); 
startActivityForResult(localIntent2, 0);
<6>调用系统的语音识别
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");  
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
其回调函数为:
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {  
// Fill the list view with the strings the recognizer thought it could have heard  
ArrayList matches = data.getStringArrayListExtra(  
RecognizerIntent.EXTRA_RESULTS);  
mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,  
matches));  
}  
super.onActivityResult(requestCode, resultCode, data);  
}  

例如:根据返回图像文件的URI,获得其绝对路径

方法如下:

Uri picPath = data.getData();

String absolutePath = getImageAbsolutePath(picPath));

protected String getImageAbsolutePath(Uri uri) {
	String[] proj = { MediaStore.Images.Media.DATA };
	Cursor cursor = managedQuery(uri, proj, null, null, null);
	int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	cursor.moveToFirst();
	return cursor.getString(column_index);
}
举报

相关推荐

0 条评论