2019-01-15 15:02:50 1769瀏覽
今天扣丁學(xué)堂Android培訓(xùn)老師給大家介紹一下關(guān)于Android獲取其他應(yīng)用中的assets資源詳解,首先有這樣一個需求:A應(yīng)用在一定條件下出發(fā)某個邏輯后,需要從B應(yīng)用中獲取一些資源(assets下的mp4視頻、還有drawable下的一些圖片用作背景),具體需求就不說啦哈哈,用一張圖來表示應(yīng)該更明白:
@Override public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException { return mBase.createPackageContext(packageName, flags); }
public static final int CONTEXT_INCLUDE_CODE = 0x00000001; public static final int CONTEXT_IGNORE_SECURITY = 0x00000002;
//栗子:獲取一個drawable 的id int identifier = bContext.getResources().getIdentifier("bg", "drawable", bContext.getPackageName());
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("GFZY", "onCreate: "); try { Context bContext = this.createPackageContext("pers.jibai.matrixtext" , Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); ClassLoader loader = bContext.getClassLoader(); Class<?> clazz = loader.loadClass("pers.jibai.matrixtext.A"); Object a = clazz.getConstructor().newInstance(); Method getAssetBg = clazz.getMethod("getBgMp4", Context.class); Log.e("GFZY", "onCreate: " + getAssetBg.getName()); InputStream invoke = (InputStream) getAssetBg.invoke(a, bContext); BufferedReader reader = new BufferedReader(new InputStreamReader(invoke)); String s = reader.readLine(); reader.close(); ((TextView) findViewById(R.id.t)).setText(s); } catch (Exception e) { e.printStackTrace(); Log.e("GFZY", "onCreate: " + e.getMessage()); } }
public class A { public void asd() { Log.e("GFZY", "asd:我是matrix "); } public InputStream getBgMp4(Context context) { try { return context.getAssets().open("asd"); } catch (IOException e) { e.printStackTrace(); } return null; } }
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開發(fā)技術(shù)”的相關(guān)資訊>>