使用Intent方式進行跨程序訪問
阿新 • • 發佈:2019-01-11
直接上程式碼啦
(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>