Android 组件复用和进程关系
2010-09-10 01:06:00 来源:WEB开发网一:跨进程调用Activity
案例设计, 设计两个Application, 第一个Application1将调用第二个Application中的Activity2, 同时第二个Application2会调用自身的Activity2. 以此判断进程ID来观察进程的存在情况, 详细说明和代码如下:
Application1: ClientApk 这个程序有一个ClientMainActivity会发起对应用二的一个Activity(名称为ServerActivity)的调用, ClientMainActivity启动时, 将进程的id打印出来
view plaincopy to clipboardprint?
public class ClientMainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.ClientTV);
ClickAction l = new ClickAction();
tv.setOnClickListener(l);
int myProcessID = Process.myPid();
//打印当前进程的ID
Log.i("---ClientMainActivity----", myProcessID + "");
}
class ClickAction implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent("com.ostrichmyself.tiantian");
//ComponetName不描述清楚的话, 就无法正确的找到ServerActivity, 但系统提供的Activity,如main都可以为什么?
final ComponentName cn = new ComponentName("com.ostrichmyself","com.ostrichmyself.ServerActivity");
intent.setComponent(cn);
startActivity(intent);
}
}
}
public class ClientMainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.ClientTV);
ClickAction l = new ClickAction();
tv.setOnClickListener(l);
更多精彩
赞助商链接