展会信息港展会大全

android在当前程序中调用其他程序
来源:互联网   发布日期:2016-01-14 09:20:02   浏览:1859次  

导读:在eclipse开发中,如果是同一个项目,如果调用不同的Activity,那么一般用Intent即可,相信大家都比较熟悉。一般的调用形式如下:Intent intent = new Intent();intent.setClass(A_Activity.this, B_Activi......

在eclipse开发中,如果是同一个项目,如果调用不同的Activity,那么一般用Intent即可,相信大家都比较熟悉。一般的调用形式如下:

Intent intent = new Intent();

intent.setClass(A_Activity.this, B_Activity.class);

startActivity(intent);

如果是不同的程序、不同的项目、或者在不同包,也依然可以使用Intent。例如:现在流行的条形码扫描,我们并不需要自己去完成扫描条形码的代码或者相应的功能模块,我们只需要在自己的程序中调用条形码扫描程序,并且在扫描后将结果返回给自己的程序即可。

项目实例:

现在,退出程序B,然后启动程序A:

在程序A中调用程序B,并且在程序B中进行一定的操作,把结果返回给程序A:

完整代码:布局文件只定义了TextView和Button,略去。

程序A:

package com.xsjayz.appa;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

/**

* 两个不同的程序彼此调用。

*

* @version 2012-09-10

*/

public class A_Activity extends Activity

{

private Button button;

private TextView textView;

public static final int requestCode01 = (int) 0x1001;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textView = (TextView) this.findViewById(R.id.TextView01);

button = (Button) this.findViewById(R.id.Button01);

button.setOnClickListener(new Button.OnClickListener()

{

@Override

public void onClick(View arg0)

{

try

{

Intent intent = new Intent();

// 传入要调用的另一个程序B的package名称及package名称加class名称

intent.setClassName("com.xsjayz.appb", "com.xsjayz.appb.B_Activity");

// 将数据传给另一个程序B

Bundle bundle = new Bundle();

bundle.putString("STR_INPUT", "提示:这是来自A_Activity的消息...");

intent.putExtras(bundle);

startActivityForResult(intent, requestCode01);

}

catch (Exception e)

{

e.printStackTrace();

textView.setText("调用前请安装程序B!");

}

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data)

{

if (resultCode != RESULT_OK)

{

return;

}

switch (requestCode)

{

case (requestCode01):

// 取得程序B返回的数据

String contents = data.getStringExtra("appb_info");

textView.setText("来自程序B的数据:\n\n" + contents);

break;

}

super.onActivityResult(requestCode, resultCode, data);

}

}

程序B:

package com.xsjayz.appb;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class B_Activity extends Activity

{

private TextView textView;

private Button button;

private String strRet = "";

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textView = (TextView) this.findViewById(R.id.TextView01);

button = (Button) this.findViewById(R.id.Button01);

try

{

// 取得程序A传过来的数据

Bundle bunde = getIntent().getExtras();

strRet = bunde.getString("STR_INPUT");

textView.setText(strRet);

}

catch (Exception e)

{

// 当直接运行程序B:不是由程序A调用

button.setEnabled(false);

textView.setText("当前程序B的状态:单独启动运行...");

}

button.setOnClickListener(new Button.OnClickListener()

{

@Override

public void onClick(View v)

{

// 将数据传给程序A

Bundle bundle = new Bundle();

bundle.putString("appb_info", "这是从程序B返回的消息:\n" + "这是来自于程序B的条形码扫描结果。");

Intent intent = new Intent();

intent.putExtras(bundle);

setResult(RESULT_OK, intent);

finish();

}

});

}

}

赞助本站

人工智能实验室

相关热词: 调用

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港