无法使用admobs横幅运行cocos2dx游戏

时间:2017-05-21 11:26:48

标签: android android-studio cocos2d-x cocos2d-x-3.0

我尝试通过android studio在cocos2dx项目中实现AdMob,并在构建成功后收到以下消息。该应用程序无法运行。我在android studio文件夹中设置了gradle,manifest文件,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();
    }

2 个答案:

答案 0 :(得分:2)

在此活动的AndroidManifest.xml文件中设置android:exported =“true”。

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

</activity>

答案 1 :(得分:1)

Activity移除AndroidManifest.xml下方,不需要显示横幅广告。

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

第二件事你从哪里调用/开始com.test.puzzle/com.google.android.gms.ads.AdActivity

的新意图