无法 运行 带有 admobs 横幅的 cocos2dx 游戏

Unable to run cocos2dx game with admobs banner

我尝试通过android studio在cocos2dx项目中实现一个AdMob,构建成功后得到如下信息。该应用程序没有 运行。我已经在 android studio 文件夹中设置了 gradle、清单文件和 AppActivity。

Starting: Intent { cmp=com.test.puzzle/com.google.android.gms.ads.AdActivity }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.sample.mygame/com.google.android.gms.ads.AdActivity } from null (pid=27372, uid=2000) not exported from uid 10466
    at android.os.Parcel.readException(Parcel.java:1472)
    at android.os.Parcel.readException(Parcel.java:1426)
    at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2174)
    at com.android.commands.am.Am.runStart(Am.java:680)
    at com.android.commands.am.Am.onRun(Am.java:270)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:76)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
    at dalvik.system.NativeStart.main(Native Method)

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.puzzle"
    android:installLocation="auto">

    <uses-feature android:glEsVersion="0x00020000" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:label="@string/app_name"
        android:allowBackup="true"
        android:icon="@mipmap/puzzle">

        <!-- Tell Cocos2dxActivity the name of our .so-->
        <meta-data android:name="android.app.lib_name"
                   android:value="MyGame" />

        <meta-data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version"/>
        <activity android:name="com.google.android.gms.ads.AdActivity"
                  android:exported="true"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <activity
            android:name="org.cocos2dx.cpp.AppActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AppActivity

package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.google.android.gms.ads.*;

public class AppActivity extends Cocos2dxActivity {

    private AdView _adView;
    private final String AD_UNIT_ID = "xxx";  

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        FrameLayout.LayoutParams adParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        adParams.gravity = (Gravity.TOP | Gravity.CENTER); 

        _adView = new AdView(this);
        _adView.setAdSize(AdSize.BANNER);
        _adView.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)    
                .addTestDevice("Test Device ID")                
                .build();

        _adView.loadAd(adRequest);
        _adView.setBackgroundColor(Color.TRANSPARENT);
        addContentView(_adView, adParams);

    }

    @Override
    protected void onPause() {
        if (_adView != null) {
            _adView.pause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (_adView != null) {
            _adView.resume();
        }
    }

    @Override
    protected void onDestroy() {
        _adView.destroy();
        super.onDestroy();
    }

在这个 Activity 的 AndroidManifest.xml 文件中设置 android:exported="true"。

<activity
 ----
android:exported="true">

</activity>

从您的 AndroidManifest.xml 中删除以下 Activity,不需要显示横幅广告。

<activity android:name="com.google.android.gms.ads.AdActivity"
          android:exported="true"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

第二件事你在哪里 calling/starting com.test.puzzle/com.google.android.gms.ads.AdActivity

的新 Intent