我已经在布局XML文件中添加了admob AdView,并且可以在android studio的设计部分中看到测试广告,但是在运行项目后,该广告不会显示在设备上。
我已经初始化了广告并将其加载到Java活动文件中,但是测试广告仍然没有显示。
这是我的代码和logcat。
activity_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/black"
android:gravity="center">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
android:layout_alignParentTop="true"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/adView"
android:layout_marginTop="20dp"
android:layout_centerVertical="true" />
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:alpha="0.9"
android:background="@android:color/darker_gray">
<LinearLayout
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingBottom="10dp">
<ImageButton
android:id="@+id/img_btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_share_black" />
</LinearLayout>
<LinearLayout
android:id="@+id/btn_re_post"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingBottom="10dp">
<ImageButton
android:id="@+id/img_re_post"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/whatsapp_logo" />
</LinearLayout>
<LinearLayout
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingBottom="10dp"
android:visibility="gone">
<ImageButton
android:id="@+id/img_btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_delete" />
</LinearLayout>
<LinearLayout
android:id="@+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingBottom="10dp"
android:visibility="gone">
<ImageButton
android:id="@+id/img_btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_download" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ImageViewerAcitviy.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import com.example.whatsappstatussaver.R;
import com.example.whatsappstatussaver.utils.Config;
//import com.example.whatsappstatussaver.utils.Observer;
//import com.example.whatsappstatussaver.utils.Subject;
import com.bumptech.glide.Glide;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
public class ImageViewerActivity extends AppCompatActivity {
private final String TAG = "ImageViewer";
String atype = "";
public LinearLayout btn_download;
public LinearLayout btn_re_post;
public LinearLayout btn_share;
public int count = 6;
DrawerActivity drawer = new DrawerActivity();
ImageView imageView;
String image_path = "";
public ImageButton img_btn_download;
public ImageButton img_btn_share;
public ImageButton img_re_post;
String listenerValue = "";
// public List<Observer> observers;
String package_name = "";
String path = "";
String type = "";
AdView mAdView;
/* access modifiers changed from: protected */
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView((int) R.layout.activity_image);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
this.imageView = (ImageView) findViewById(R.id.imageView);
// this.observers = new ArrayList();
Intent intent = getIntent();
if (intent != null) {
this.image_path = intent.getStringExtra("image");
this.type = intent.getStringExtra("type");
this.atype = intent.getStringExtra("atype");
if (this.image_path != null) {
Glide.with((FragmentActivity) this).load(this.image_path).into(this.imageView);
}
}
if (this.type.equals("0")) {
this.path = Config.WhatsAppSaveStatus;
this.package_name = "com.whatsapp";
Log.i("WhatsAppSaveStatus", this.path);
} else if (this.type.equals("1")) {
this.path = Config.GBWhatsAppSaveStatus;
this.package_name = "com.gbwhatsapp";
Log.i("GBWhatsAppSaveStatus", this.path);
} else if (this.type.equals("2")) {
this.path = Config.WhatsAppBusinessSaveStatus;
this.package_name = "com.whatsapp.w4b";
Log.i("WhatsAppBusinessSave", this.path);
}
this.btn_download = (LinearLayout) findViewById(R.id.btn_download);
this.btn_download.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ImageViewerActivity.this.copyFileOrDirectory(ImageViewerActivity.this.image_path, ImageViewerActivity.this.path);
ImageViewerActivity.this.sharePerAds();
}
});
this.img_btn_download = (ImageButton) findViewById(R.id.img_btn_download);
this.img_btn_download.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ImageViewerActivity.this.copyFileOrDirectory(ImageViewerActivity.this.image_path, ImageViewerActivity.this.path);
ImageViewerActivity.this.sharePerAds();
}
});
this.btn_share = (LinearLayout) findViewById(R.id.btn_share);
this.btn_share.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (ImageViewerActivity.this.image_path.endsWith(".jpg")) {
ImageViewerActivity.this.shareVia("image/jpg", ImageViewerActivity.this.image_path);
} else if (ImageViewerActivity.this.image_path.endsWith(".mp4")) {
ImageViewerActivity.this.shareVia("video/mp4", ImageViewerActivity.this.image_path);
}
ImageViewerActivity.this.sharePerAds();
}
});
this.img_btn_share = (ImageButton) findViewById(R.id.img_btn_share);
this.img_btn_share.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (ImageViewerActivity.this.image_path.endsWith(".jpg")) {
ImageViewerActivity.this.shareVia("image/jpg", ImageViewerActivity.this.image_path);
} else if (ImageViewerActivity.this.image_path.endsWith(".mp4")) {
ImageViewerActivity.this.shareVia("video/mp4", ImageViewerActivity.this.image_path);
}
ImageViewerActivity.this.sharePerAds();
}
});
this.btn_re_post = (LinearLayout) findViewById(R.id.btn_re_post);
this.btn_re_post.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (ImageViewerActivity.this.image_path.endsWith(".jpg")) {
ImageViewerActivity.this.shareViaWhatsApp("image/jpg", ImageViewerActivity.this.image_path, ImageViewerActivity.this.package_name);
} else if (ImageViewerActivity.this.image_path.endsWith(".mp4")) {
ImageViewerActivity.this.shareViaWhatsApp("video/mp4", ImageViewerActivity.this.image_path, ImageViewerActivity.this.package_name);
}
ImageViewerActivity.this.sharePerAds();
}
});
this.img_re_post = (ImageButton) findViewById(R.id.img_re_post);
this.img_re_post.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (ImageViewerActivity.this.image_path.endsWith(".jpg")) {
ImageViewerActivity.this.shareViaWhatsApp("image/jpg", ImageViewerActivity.this.image_path, ImageViewerActivity.this.package_name);
} else if (ImageViewerActivity.this.image_path.endsWith(".mp4")) {
ImageViewerActivity.this.shareViaWhatsApp("video/mp4", ImageViewerActivity.this.image_path, ImageViewerActivity.this.package_name);
}
ImageViewerActivity.this.sharePerAds();
}
});
if (this.atype.equals("0")) {
this.btn_download.setVisibility(View.GONE);
} else if (this.atype.equals("1")) {
this.btn_download.setVisibility(View.VISIBLE);
}
sharePerAds();
}
public void copyFileOrDirectory(String str, String str2) {
try {
File file = new File(str);
File file2 = new File(str2, file.getName());
if (file.isDirectory()) {
for (String file3 : file.list()) {
copyFileOrDirectory(new File(file, file3).getPath(), file2.getPath());
}
return;
}
copyFile(file, file2);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onDestroy() {
if (this.mAdView != null) {
this.mAdView.destroy();
}
super.onDestroy();
}
public void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists())
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
Toast.makeText(getApplicationContext(), "Picture Saved", Toast.LENGTH_SHORT).show();
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
public void shareVia(String str, String str2) {
Intent intent = new Intent("android.intent.action.SEND");
intent.setType(str);
intent.putExtra("android.intent.extra.STREAM", Uri.fromFile(new File(str2)));
startActivity(Intent.createChooser(intent, "Share via"));
}
public void shareViaWhatsApp(String str, String str2, String str3) {
try {
getPackageManager().getPackageInfo(str3, PackageManager.GET_META_DATA);
Intent intent = new Intent("android.intent.action.SEND");
intent.setType(str);
intent.putExtra("android.intent.extra.STREAM", Uri.fromFile(new File(str2)));
intent.setPackage(str3);
startActivity(Intent.createChooser(intent, "Share via"));
} catch (PackageManager.NameNotFoundException unused) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
}
public void allSharedPreference(int i) {
SharedPreferences.Editor edit = getSharedPreferences("PREFRENCE", 0).edit();
edit.putString("ALL", String.valueOf(i));
edit.commit();
}
public void sharePerAds() {
int i = 1;
if (Config.getALLState(this).length() > 0) {
i = Integer.parseInt(Config.getALLState(this));
if (i > this.count) {
allSharedPreference(0);
} else {
i++;
allSharedPreference(i);
}
} else {
allSharedPreference(1);
}
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.whatsappstatussaver">
<!-- <uses-sdk -->
<!-- android:minSdkVersion="16" -->
<!-- android:targetSdkVersion="30" /> -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".utils.Application"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.AdDemo"></activity>
<!-- android:requestLegacyExternalStorage="true" -->
<activity
android:name=".activity.ImageViewerActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
</application>
</manifest>
buildgradle(应用程序):
apply plugin: 'com.android.application'
//apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.whatsappstatussaver"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
configurations {
all*.exclude group: 'com.google.firebase', module: 'firebase-common'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.github.bumptech.glide:glide:4.11.0'
// implementation 'com.google.firebase:firebase-ads:19.0.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'org.jsoup:jsoup:1.11.3'
implementation 'com.google.android.gms:play-services-ads:19.3.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Logcat:
com.example.whatsappstatussaver, 1402,
batch {
bundle {
protocol_version: 1
platform: android
gmp_version: 31028
uploading_gmp_version: 203019
dynamite_version: 16
config_version: -1
gmp_app_id:
admob_app_id: ca-app-pub-3940256099942544~3347511713
app_id: com.example.whatsappstatussaver
app_version: 1.0
app_version_major: 1
firebase_instance_id:
dev_cert_hash: 4629537210627045213
app_store: pc
upload_timestamp_millis: 1598703946747
start_timestamp_millis: 1598703934340
end_timestamp_millis: 1598703934340
app_instance_id: ce7f10ffa66ccd0e4b9e7b90abe45d83
resettable_device_id: 1c1944ae-94b5-4a65-8f96-63a1db9cf843
device_id:
ds_id:
limited_ad_tracking: false
os_version: 9
device_model: RMX1805
user_default_language: en-us
time_zone_offset_minutes: 330
bundle_sequential_index: 1
service_upload: true
health_monitor:
user_property {
set_timestamp_millis: 1598703934340
name: first_open_time(_fot)
string_value:
int_value: 1598706000000
}
user_property {
set_timestamp_millis: 1598703934340
name: first_open_after_install(_fi)
string_value:
int_value: 1
}
user_property {
set_timestamp_millis: 1598703946752
name: lifetime_user_engagement(_lte)
string_value:
int_value: 1
}
event {
name: first_open(_f)
timestamp_millis: 1598703934340
previous_timestamp_millis: 0
param {
name: ga_conversion(_c)
int_value: 1
}
param {
name: ga_event_origin(_o)
string_value: auto
}
param {
name: _r
int_value: 1
}
param {
name: engagement_time_msec(_et)
int_value: 1
}
param {
name: previous_first_open_count(_pfo)
int_value: 13
}
param {
name: system_app(_sys)
int_value: 0
}
param {
name: update_with_analytics(_uwa)
int_value: 0
}
param {
name: system_app_update(_sysu)
int_value: 0
}
}
event {
name: user_engagement(_e)
timestamp_millis: 1598703934340
previous_timestamp_millis: 0
param {
name: ga_event_origin(_o)
string_value: auto
}
param {
name: engagement_time_msec(_et)
int_value: 1
}
param {
name: _fr
int_value: 1
}
}
}
bundle {
protocol_version: 1
platform: android
gmp_version: 31028
uploading_gmp_version: 203019
dynamite_version: 16
config_version: -1
gmp_app_id:
admob_app_id: ca-app-pub-3940256099942544~3347511713
app_id: com.example.whatsappstatussaver
app_version: 1.0
app_version_major: 1
firebase_instance_id:
dev_cert_hash: 4629537210627045213
app_store: pc
upload_timestamp_millis: 1598703946747
start_timestamp_millis: 1598703936107
end_timestamp_millis: 1598703946339
previous_bundle_start_timestamp_millis: 1598703934340
previous_bundle_end_timestamp_millis: 1598703934340
app_instance_id: ce7f10ffa66ccd0e4b9e7b90abe45d83
resettable_device_id: 1c1944ae-94b5-4a65-8f96-63a1db9cf843
device_id:
ds_id:
limited_ad_tracking: false
os_version: 9
device_model: RMX1805
user_default_language: en-us
time_zone_offset_minutes: 330
bundle_sequential_index: 2
service_upload: true
health_monitor:
user_property {
set_timestamp_millis: 1598703934340
name: first_open_time(_fot)
string_value:
int_value: 1598706000000
}
user_property {
set_timestamp_millis: 1598703934340
name: first_open_after_install(_fi)
string_value:
int_value: 1
}
user_property {
set_timestamp_millis: 1598703936107
name: ga_session_number(_sno)
string_value:
int_value: 1
}
user_property {
set_timestamp_millis
2020-08-29 17:55:54.565 3482-3723/com.example.whatsappstatussaver W/ConnectionTracker: Exception thrown while unbinding
java.lang.IllegalArgumentException: Service not registered: ly@b95b675
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1723)
at android.app.ContextImpl.unbindService(ContextImpl.java:1759)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:728)
at ci.b(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):1)
at ci.a(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):6)
at lz.A(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):10)
at lk.a(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):3)
at eb.run(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):3)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at jf.run(:com.google.android.gms.dynamite_measurementdynamite@203019066@20.30.19 (100406-0):6)