使用components有兩種方式:java
ComponentName com = new ComponentName(MainActivity.this,MyActivity.class);//first ComponentName com = new ComponentName("com.example.testcomponent","com.example.testcomponent.MyActivity");//second
第二種方式的第二個參數必須寫全包名,否則會報錯的。ide
同樣的,主Activity中:this
button.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); ComponentName com = new ComponentName("com.example.testcomponent", "com.example.testcomponent.MyActivity"); intent.setComponent(com); String s = "This is a Test."; intent.putExtra("id", s); startActivity(intent); } });
在目標Ativity中獲取數據和上面是同樣的,可是,在目標Activity中就能夠獲取到源目標,知道是從哪兒傳遞過來的:spa
text = (TextView)findViewById(R.id.text); Intent intent = getIntent(); String str = intent.getStringExtra("id"); ComponentName com = intent.getComponent(); String pkgName = com.getPackageName(); String className = com.getClassName(); text.setText(str + "\n" + pkgName + "\n" + className);
參考資料:http://blog.csdn.net/sunhengzhi_212/article/details/8274834.net