我创建了一个新的示例项目,以在活动底部显示Facebook受众网络横幅广告。
我的应用程序正在设备中运行,但底部没有显示任何内容。我已经添加了测试设备的哈希值。
MainActivity.java
package liveradio.radioz.com.facebookads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.facebook.ads.*;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, "PLACEMENT-ID", AdSize.BANNER_HEIGHT_50);
// Find the Ad Container
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
// Add the ad view to your activity layout
adContainer.addView(adView);
AdSettings.addTestDevice("DEVICE-ID");
// Request an ad
adView.loadAd();
}
@Override
protected void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="liveradio.radioz.com.facebookads.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
/>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="liveradio.radioz.com.facebookads">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志:
01/12 12:13:01: Launching app
$ adb shell am start -n "liveradio.radioz.com.facebookads/liveradio.radioz.com.facebookads.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: liveradio.radioz.com.facebookads.test | liveradio.radioz.com.facebookads
Waiting for application to come online: liveradio.radioz.com.facebookads.test | liveradio.radioz.com.facebookads
Waiting for application to come online: liveradio.radioz.com.facebookads.test | liveradio.radioz.com.facebookads
Connecting to liveradio.radioz.com.facebookads
Waiting for application to start debug server
Waiting for application to come online: liveradio.radioz.com.facebookads.test | liveradio.radioz.com.facebookads
Connecting to liveradio.radioz.com.facebookads
Connected to the target VM, address: 'localhost:8645', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Not late-enabling -Xcheck:jni (already on)
W/zygote: Suspending all threads took: 83.944ms
W/zygote: Unexpected CPU variant for X86 using defaults: x86
I/zygote: Background concurrent copying GC freed 9752(4MB) AllocSpace objects, 0(0B) LOS objects, 61% free, 973KB/2MB, paused 48.188ms total 236.235ms
W/ActivityThread: Application liveradio.radioz.com.facebookads is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1486)
I/InstantRun: starting instant run server: is main process
I/WebViewFactory: Loading com.android.chrome version 61.0.3163.98 (code 316409812)
I/zygote: The ClassLoaderContext is a special shared library.
I/cr_LibraryLoader: Time to load native libraries: 3 ms (timestamps 6510-6513)
I/chromium: [INFO:library_loader_hooks.cc(136)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "61.0.3163.98", actual native library version number "61.0.3163.98"
E/FBAudienceNetwork: You don't call AudienceNetworkAds.initialize(). Some functionality may not work properly.
D/IS_UNITY: false
D/AdInternalSettings: Test mode device hash: 877dd37b-fff7-4fa1-b83d-776592220359
D/AdInternalSettings: When testing your app with Facebook's ad units you must specify the device hashed ID to ensure the delivery of test ads, add the following code before loading an ad: AdSettings.addTestDevice("877dd37b-fff7-4fa1-b83d-776592220359");
D/IS_UNITY: false
E/ActivityThread: Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider
D/OpenGLRenderer: HWUI GL Pipeline
V/StudioProfiler: StudioProfilers agent attached.
V/StudioProfiler: Acquiring Application for Events
V/StudioProfiler: Transformed class: java/net/URL
W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
V/StudioProfiler: Memory control stream started.
[ 01-12 12:13:06.606 20774:20877 D/ ]
HostConnection::get() New Host Connection established 0xd1835a80, tid 20877
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xe4b85540: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0xe4b85540: ver 3 0 (tinfo 0xe4b83360)
D/EGL_emulation: eglMakeCurrent: 0xe4b85540: ver 3 0 (tinfo 0xe4b83360)
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
[ 01-12 12:13:07.026 20774:20774 D/ ]
HostConnection::get() New Host Connection established 0xd1583c00, tid 20774
D/EGL_emulation: eglCreateContext: 0xcecec360: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0xcecec360: ver 3 0 (tinfo 0xe21fff60)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/cr_CrashFileManager: /data/user/0/liveradio.radioz.com.facebookads/cache/WebView/Crash Reports does not exist or is not a directory
W/com.facebookads: type=1400 audit(0.0:22): avc: denied { read } for name="vmstat" dev="proc" ino=4026532039 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0
D/IS_UNITY: false
I/zygote: Background concurrent copying GC freed 7886(1025KB) AllocSpace objects, 5(100KB) LOS objects, 50% free, 1746KB/3MB, paused 680us total 122.639ms
I/zygote: Do partial code cache collection, code=30KB, data=25KB
I/zygote: After code cache collection, code=30KB, data=25KB
I/zygote: Increasing code cache capacity to 128KB
V/StudioProfiler: Live memory tracking disabled.
V/StudioProfiler: Live memory tracking enabled.
V/StudioProfiler: JNIEnv not attached
V/StudioProfiler: Loaded classes: 6311
V/StudioProfiler: Tracking initialization took: 648527000ns
I/zygote: Do partial code cache collection, code=60KB, data=44KB
I/zygote: After code cache collection, code=56KB, data=43KB
I/zygote: Increasing code cache capacity to 256KB
Disconnected from the target VM, address: 'localhost:8645', transport: 'socket'
我获得了成功的构建并将其安装在设备中。打开应用程序时,我的主要活动底部看不到任何广告或测试广告。
答案 0 :(得分:3)
如果您已添加受众群体网络库,则可以添加以下代码来显示测试广告,而不必担心添加测试设备。
if (BuildConfig.DEBUG) {
AdSettings.setTestMode(true);
}
最好在您的Application类中添加此代码。
答案 1 :(得分:1)
首先实现此库:
implementation 'com.facebook.android:audience-network-sdk:4.99.1'
比使用下面的代码:
com.facebook.ads.AdView adView;
adView = new com.facebook.ads.AdView(this, "PLACEMENT-ID",
com.facebook.ads.AdSize.BANNER_HEIGHT_50);
((LinearLayout) adContainer).addView(adView);
adView.loadAd();