1. 程式人生 > >監聽Google Player下載並獲取包名

監聽Google Player下載並獲取包名

plink sed 步驟 tco elf app 應用 per his

1 解決思路

通過監聽ContentObserver監聽下載路徑content://downloads

2 具體步驟

2.1 設置監聽器

    context.getContentResolver()
        .registerContentObserver(DOWNLOAD_URI, true, new ContentObserver() {
            @Override
            public void onChange(boolean selfChange, Uri uri) {
                super.onChange(selfChange, uri);
                parseData(uri);
            }
        });

2.2 數據解析

    public void parseData(Uri uri) {
        List<String> segments = uri.getPathSegments();
        if (segments != null && segments.size() > 1) {
            String segment = (String) segments.get(1);
            synchronized (this.pathSegments) {
                if (this.pathSegments.contains(segment)) {
                    return;
                }
                this.pathSegments.add(segment);
            }

            this.obtainAppInfo(segment);
        }
    }

應用信息獲取:

    private void obtainAppInfo(String segment){
        ...
        cursor = this.queryDownloadPkg(downloadUri);
        if (cursor == null || cursor.getColumnCount() < 15) {
            return;
        }

        cursor.moveToNext();
        String marketName = cursor.getString(9); // 應用市場包名
        String appLink = cursor.getString(15); // 下載應用的包名鏈接
        String appName = cursor.getString(20); //獲取應用名稱
        ...
    }

監聽Google Player下載並獲取包名