android 获取渠道号

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27247601/article/details/83619671

上文我已经说过,我们采用的打渠道包的方式是在apk文件中写入一个空的文件来标识不同的渠道包,这也是大多数打渠道包的原理。把渠道包解压之后,在META-INF文件夹下就会多出以下框框的中文件:

既然我们已经写入了文件,下面就是要怎么读取这个文件和截取渠道号了。代码如下:

public static String getChannelName(){

String sourceDir = TestApplication.getApp().getPackageCodePath();
ZipFile zipfile = null;
try {
    zipfile = new ZipFile(sourceDir);
    Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        String entryName = entry.getName();
        if (entryName.startsWith("META-INF/channel_")) {
            CHANNEL_NAME = entryName.substring(17, entryName.length());
           
        }
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (zipfile != null) {
        try {
            zipfile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

注意:渠道包需要重新签名,否者会安装失败!

猜你喜欢

转载自blog.csdn.net/qq_27247601/article/details/83619671