利用meta-data的數據,動態更改contetnprovider的authorities

 因爲業務需要,做個庫給應用使用,需要動態的給contentprovider配置uri,於是便用到了meta和${applicationId},很方便的解決了問題,並且極大的方便了其他應用的使用。

1.在AndroidManifest裏面配置meta

 <meta-data
    android:name="BFC_UPLOAD_HOST_APP_ID"
    android:value="${applicationId}" />
  ${applicationId}的作用就是用applicationId替代meta-data 的value的值
2.contentprovider配置
  <provider
    android:name="com.eebbk.bfc.uploadsdk.upload.provider.UploadProvider"
    android:authorities="${applicationId}.bbkupload"
    android:exported="true">
  </provider>
3.獲取meta-data的值
    public static String getHostAppId(Context appContext) throws IllegalArgumentException {
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA);
        if(applicationInfo == null){
            throw new IllegalArgumentException(" get application info = null, has no meta data! ");
        }
        return applicationInfo.metaData.getString(Constants.BFC_UPLOADLOAD_HOST_APP_ID);
         } catch (PackageManager.NameNotFoundException e) {
        throw new IllegalArgumentException(" get application info error! ", e);
    }
  }
5.爲packagename賦值,並獲取uri動態的string:
    String packagename = applicationInfo ;
   String uri= TextUtils.concat(packagename , ".bbkupload").toString()
6.獲取到authorties 的值後。我們便可以在contentprovider創建時配置到URI裏面,
  應用使用的時候也不用配置contentprovider了
  示例如下:URI_MACHER.addURI(uri, "my_uploads", MY_UPLOADS);


  


發佈了49 篇原創文章 · 獲贊 35 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章