当前位置:网站首页>谷歌AdMob广告学习
谷歌AdMob广告学习
2022-04-23 06:07:00 【独饮敌敌畏丶】
学的杂七杂八的东西
1.ss协议是什么
我总结一下,ss/ssr的目的就是用来翻q的,侧重于流量混淆加密,穿透防火q是第一位,使用socket5代理。而vpn的目的是用来加密企业数据的,安全是第一位,本意并非是翻q
对于vpn来说安全是第一位的,而对于ss/ssr来说穿透防火q是第一位,抗干扰性强,而且对流量做了混淆,(所有流量在通过防火q的时候,基本上都被识别为普通流量,也就是说你翻q了,但是相关部门是检测不到你在翻q的。)两者的出发点和着重点就不同,ss/ssr更注重流量的混淆加密。
参考文章
2. 配置本地token,作为拉取github项目的凭证
一.谷歌AdMob
注意内存泄漏问题
1.谷歌AdMob是什么
AdMob 是移动电话广告市场,可提供用户在移动电话网络上播放广告。2006年创建,2009年被谷歌收购。
Admob覆盖200多个国家,是美国最大的手机广告平台。
2.基本使用
这里建议看官方文档,非常详细,几乎是傻瓜式教学。点这里进入官方文档
我这里说一下我一开始遇到的两个坑,都很简单,但是我当时没注意到
Bug1: 加了个括号
一开始我一直在纠结这个报错,后来才发现,AdRequest
后面不用加括号。也算长个记性吧
Bug2: 两个id
这里一共有两个id,一个id是应用级id,是加在Manifest文件中的,另外一个id是控件级id,是加在控件的属性上的。
前者是
后者是
官方文档说的测试id,是后者,而不是Manifest中的
演示结果
我在广告控件里加了个TextView
效果如下
3.插页式广告demo
上面是横幅广告,是谷歌官方推荐新手学的,我用的是插页式广告。我把官方文档的demo摘取下来了。在这里我把他分析一下
xml中还是官方文档当中的布局,我这里只显示MainActivity中的
布局是这样的,最上面是广告,下面有个TextView
,左下角是个Button
,右下角是个TextView
大体流程是这样:首先启动Activity
,执行生命周期函数,然后在onCreate
中进行初始化和广告对象InterstitialAd
的赋值,然后利用倒计时工具CountDownTimer
,倒计时3s后(期间右下角的TextView
会更新剩余时间,从3s开始,直到0s),左边的Button
才显示出来。点击Button
的时候,会先判断广告对象是否为null
,如果不为null
,就显示广告,如果为null
,就重新加载广告,然后倒计时3s,结束后,显示按钮。
注意点1
在这里我做了个测试,就是切换到广告页面和切回MainActivity
,发现分别调用了onPause
和onResume
。所以我猜测,广告页面不是新的Activity
,切回的时候,会调用Activity
的生命周期函数onResume
执行效果如下
执行过程中,日志打印以及分析如下
注意点2
切回MainActivity
的时候,广告对象InterstitialAd
又为null
了。我猜测是调用广告页面的时候,即调用show
方法的时候,会对广告对象赋值为null
。
MainActivity的完整代码如下
package com.example.mypreworktest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
private static final long GAME_LENGTH_MILLISECONDS = 3000;//总时长
private InterstitialAd interstitialAd;
private CountDownTimer countDownTimer;
private Button retryButton;
private boolean gameIsInProgress;
private long timerMilliseconds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadAd();
// Create the "retry" button, which tries to show an interstitial between game plays.
retryButton = findViewById(R.id.retry_button);
retryButton.setVisibility(View.INVISIBLE);
retryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInterstitial();
}
});
startGame();
}
public void loadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
//插页广告 加载
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
// 翻译: 在此之前,interstitialAd都为null,此时才被加载
MainActivity.this.interstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
Toast.makeText(MainActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
//FullScreenContentCallback 处理与展示 InterstitialAd 相关的事件
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't show it a second time.
// 翻译: 确保将引用设置为null,这样就不会再次显示它
MainActivity.this.interstitialAd = null;
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
//翻译: 同上
MainActivity.this.interstitialAd = null;
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
interstitialAd = null;
String error =
String.format(
"domain: %s, code: %d, message: %s",
loadAdError.getDomain(), loadAdError.getCode(), loadAdError.getMessage());
Toast.makeText(
MainActivity.this, "onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
.show();
}
});
}
private void createTimer(final long milliseconds) {
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
//翻译: 它倒计时到级别的末尾,并显示“重试”按钮。
if (countDownTimer != null) {
countDownTimer.cancel();
}
final TextView textView = findViewById(R.id.timer);
countDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUnitFinished) {
timerMilliseconds = millisUnitFinished;
Log.i(TAG, millisUnitFinished + "ms");
textView.setText("seconds remaining: " + ((millisUnitFinished / 1000) + 1));
}
@Override
public void onFinish() {
gameIsInProgress = false;
textView.setText("done!");
retryButton.setVisibility(View.VISIBLE);
}
};
}
@Override
protected void onStart() {
Log.i(TAG, "onStart");
super.onStart();
}
@Override
protected void onRestart() {
Log.i(TAG, "onRestart");
super.onRestart();
}
@Override
public void onResume() {
// Start or resume the game.
super.onResume();
Log.i(TAG, "onResume,此时广告为: "+ interstitialAd);
if (gameIsInProgress) {
resumeGame(timerMilliseconds);
}
}
@Override
public void onPause() {
// Cancel the timer if the game is paused.
Log.i(TAG, "onPause");
countDownTimer.cancel();
super.onPause();
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (interstitialAd != null) {
interstitialAd.show(this);
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
startGame();
}
}
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (interstitialAd == null) {
loadAd();
}
retryButton.setVisibility(View.INVISIBLE);
resumeGame(GAME_LENGTH_MILLISECONDS);
}
private void resumeGame(long milliseconds) {
Log.i(TAG, "resumeGame");
// Create a new timer for the correct length and start it.
gameIsInProgress = true;
timerMilliseconds = milliseconds;
createTimer(milliseconds);
countDownTimer.start();
}
}
版权声明
本文为[独饮敌敌畏丶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/afdafvdaa/article/details/121207539
边栏推荐
- 记录webView显示空白的又一坑
- JS 比较2个数组中不同的元素
- Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理
- iTOP4412 FramebufferNativeWindow(4.0.3_r1)
- Prometheus monitoring method and index interpretation of influxdb
- Exception record-14
- Kubernetes coredns FAQ reference
- When switch case, concatenate the error case and if of the conventional judgment expression and use L
- Abnormal record-18
- 利用栈实现队列的出队入队
猜你喜欢
this.getOptions is not a function
iTOP4412 LCD背光驱动(PWM)
10g数据库使用大内存主机时不能启动的问题
Static interface method calls are not supported at language level ‘5‘ 异常解决
Chaos带你快速上手混沌工程
Dolphinscheduler调度spark任务踩坑记录
Binder机制原理
Winter combat camp hands-on combat - cloud essential environment preparation, hands-on practical operation, quickly build lamp environment, lead mouse cloud Xiaobao backpack without shadow
Relabel of Prometheus_ Configs and metric_ relabel_ Configs explanation and usage examples
统一任务分发调度执行框架
随机推荐
Component learning
Exception record-8
npm ERR code 500解决
解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
Ali vector library Icon tutorial (online, download)
mysql和pg库遇到冲突数据时的两种处理方式
oracle中生成32位uuid
pycharm Install packages failed
组件化学习
发布自定义插件到本地服务器
Exception record-14
oracle库恢复数据
Prometheus thanos Quick Guide
Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)
oracle对表字段的修改
oracle清除sql的缓存
[exynos4412] [itop4412] [android-k] add product options
this.getOptions is not a function
基於ECS搭建雲上博客(雲小寶碼上送祝福,免費抽iphone13任務詳解)
Chaos带你快速上手混沌工程