如何在Android 7中实施YouTube Direct Lite

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Android 7中实施YouTube Direct Lite相关的知识,希望对你有一定的参考价值。

我在GitHub(https://github.com/youtube/yt-direct-lite-android)上发现了YouTube Direct Lite(android / Java),作为如何允许用户将视频上传到YouTube播放列表的一个很好的示例。我已经在Nexus 4和Android 4.2.2上的Android 5.1.1上成功调试并发布了apk模式。在Galaxy Tab 7上。然而,在LG4上安装到Android 7.0时,我收到“连接播放服务失败”消息。 “不幸的是,I-Witness(重命名的应用程序)Uploader已停止”是应用程序崩溃之前的消息,当我选择登录帐户时。

在最初从git导入代码时,Android Studio 3.01显示了一些过时的库,我更新了这些库,并找到了解决由一个过时的库引起的httprequest / httpclient错误的方法。最大的问题围绕'com.google.android.gms:play-services-auth'和'com.google.android.gms:play-services-plus',在原始示例中为7.8。

Android Studio建议更新到当前的11.8,但当我在代码中出现了许多“红色下划线”,因此我成功使用了'com.google.android.gms:play-services-plus:9.0.0'和com.google.android.gms:发挥服务-auth的:9.0.0。

具体来说,11.8导致与以下与firebase相关的外部库发生冲突,我不明白,因为firebase未在项目中实现:

google.firebase:firebase-analytics-9.00
google.firebase:firebase-analytics-impl-9.00
google.firebase:firebase-common-9.00
google.firebase:firebase-core-9.00
google.firebase:firebase-iid-9.00

这个问题似乎围绕着身份验证,但针对特定Android版本的调试位置?

这是我的应用级build.gradle,如果有人可能熟悉YouTube direct Lite并且可以提出建议来解决Android 7上的安装问题:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.abengnews.iwitness"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-plus:9.0.0'
    implementation 'com.google.android.gms:play-services-auth:9.0.0'
    //implementation 'com.google.android.gms:play-services-identity:9.0.0'
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:support-v13:27.0.2'
    implementation 'com.google.apis:google-api-services-youtube:v3-rev120-1.19.0'
    implementation 'com.google.http-client:google-http-client-android:+'
    implementation 'com.google.api-client:google-api-client:1.23.0'
    implementation 'com.google.api-client:google-api-client-android:1.23.0'
    implementation 'com.google.api-client:google-api-client-gson:+'
    implementation 'com.google.code.gson:gson:2.2.4'
    implementation 'com.mcxiaoke.volley:library:1.0.18'
    implementation files('libs/YouTubeAndroidPlayerApi.jar')
}
configurations {
    all {
        exclude module: 'httpclient'
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}
apply plugin: 'com.google.gms.google-services'

谢谢你的帮助。

附:事实证明该问题与AndroidManifest.xml中的<uses-permission android:name="android.permission.GET_ACCOUNTS" />有关,该问题已在Android 6.1中更改。我在手机上摆弄应用程序的设置时偶然发现了它。 “联系人”是“权限”下列出的唯一选项,当我启用对联系人的访问权限时,该应用程序按预期运行。

现在我需要了解如何以编程方式实现权限

答案

有关未能连接到Play服务以及随后崩溃的消息与AndroidManifest.xml中的<uses-permission android:name="android.permission.GET_ACCOUNTS" />需要自Android 6.1以来明确授予用户权限这一事实有关。

在Android的示例运行时权限中找到以下代码,发现here已经解决了问题(虽然应用程序在安装后第一次运行时崩溃但后来没有崩溃)。

加载主活动内容视图时:

        // Verify that all required contact permissions have been granted.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
                != PackageManager.PERMISSION_GRANTED) {
            // Contacts permissions have not been granted.
            Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions.");
            requestContactsPermissions();
        }

然后请求和设置权限:

// permissions
/**
 * Id to identify a contacts permission request.
 */
private static final int REQUEST_CONTACTS = 1;

/**
 * Permissions required to read and write contacts. Used by the .
 */
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.GET_ACCOUNTS,
};

/**
 * Requests the Contacts permissions.
 * If the permission has been denied previously, a SnackBar will prompt the user to grant the
 * permission, otherwise it is requested directly.
 */
private void requestContactsPermissions() {
    // BEGIN_INCLUDE(contacts_permission_request)
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.GET_ACCOUNTS)
            ) {

        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // For example, if the request has been denied previously.
        Log.i(TAG,
                "Displaying contacts permission rationale to provide additional context.");

        // Display a SnackBar with an explanation and a button to trigger the request.
        Snackbar.make(mLayout, R.string.permission_contacts_rationale,
                Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ActivityCompat
                                .requestPermissions(MainActivity.this, PERMISSIONS_CONTACT,
                                        REQUEST_CONTACTS);
                    }
                })
                .show();
    } else {
        // Contact permissions have not been granted yet. Request them directly.
        ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
    }
    // END_INCLUDE(contacts_permission_request)
}
/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {

    if (requestCode == REQUEST_CONTACTS) {
        Log.i(TAG, "Received response for contact permissions request.");

        // We have requested multiple permissions for contacts, so all of them need to be
        // checked.
        if (PermissionUtil.verifyPermissions(grantResults)) {
            // All required permissions have been granted, display contacts fragment.
            Snackbar.make(mLayout, R.string.permision_available_contacts,
                    Snackbar.LENGTH_SHORT)
                    .show();
        } else {
            Log.i(TAG, "Contacts permissions were NOT granted.");
            Snackbar.make(mLayout, R.string.permissions_not_granted,
                    Snackbar.LENGTH_SHORT)
                    .show();
        }

    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

我确定有一些可以清理的冗余代码,因为我没有任何按钮可以点击以请求权限。

以上是关于如何在Android 7中实施YouTube Direct Lite的主要内容,如果未能解决你的问题,请参考以下文章

YouTube API - 另一项活动中的黑屏

在 Android 应用中实施 AdWords 的最佳方式是啥?

XAMARIN - 从 youtube 添加视频

在流明的 AWS SQS 中实施优先级(高、中、低)

如何在greenplum中实施CDC?

如何在 AWS Cloud Formation 中实施嵌套堆栈?