直接上代码啦
(1)这里我用一个按钮触发事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
method1();
}
});
return true;
}
/*
* 1.利用compnent方式访问其他apk的(主activity),无需设置androidmanifest.xml
*/
public void method1() {
// 这些代码是启动另外的一个应用程序的主Activity
ComponentName componetName = new ComponentName(
// 这个是另外一个应用程序的包名 ,androidmanifest.xml中的package值!!!
"com.amaker.test",
// 这个参数是要启动的Activity (主activity)
"com.amaker.test.MainActivity");
try {
Intent intent = new Intent();
intent.setComponent(componetName);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"没有找到应用程序!!!", 0).show();
}
}
/*
* 2.利用compnent方式访问其他apk的(非主activity),需要设置androidmanifest。xml中非主activity的exported属性,具体如下:
* 被访问的activity是"com.amaker.ch03.drawable.TestBitmapActivity" 则需要设置的是:<activity android:name="com.amaker.ch03.drawable.TestBitmapActivity" android:exported="true"/>
*/
public void method2() {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.amaker.test","com.amaker.ch03.drawable.TestBitmapActivity"));
//"com.amaker.ch03.string.TestStringActivity"));
//intent.setAction("android.intent.action.VIEW");
startActivity(intent);
}
/*
* 3.设置androidmanifest.xml访问其他apk的(非主activity)
* 注意:使用这种方式的时候需要在被调用的apk的androidmanifest.xml里做如下设置(访问非主activity) <activity
* android:name="com.amaker.ch03.xml.TestXmlActivity"> <intent-filter>
* <action android:name="android.intent.action.xml" /> <data
* android:scheme="info" /> <category
* android:name="android.intent.category.DEFAULT" /> </intent-filter>
* </activity>
*/
public void method3() {
Intent intent = new Intent("android.intent.action.xml",
Uri.parse("info://调用其他应用程序的Activity"));
startActivity(intent);
}
2)被调用的工程之所以可以被Demo调用,是因为AndroidManifest.xml被做了修改。具体代码如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.amaker.ch03.color.TestColorActivity">
<action android:name="android.intent.action.color" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
<activity android:name="com.amaker.ch03.string.TestStringActivity" android:exported="true"/>
<activity android:name="com.amaker.ch03.dimen.TestDimensionActivity"/>
<activity android:name="com.amaker.ch03.xml.TestXmlActivity">
<intent-filter>
<action android:name="android.intent.action.xml" />
<data android:scheme="info" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.amaker.ch03.drawable.TestBitmapActivity" android:exported="true"/>
<activity android:name="com.amaker.ch03.layout.TestLayoutActivity"/>
<activity android:name="com.amaker.ch03.menu.TestMenuActivity"/>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest> </span>
做了一个小总结,希望能给程序宝宝们提供一个便利,哈哈!!!