浏览代码

Upgrade: AdMob integration, clean-up, and Android deploy/run automation

CartBlanche 2 周之前
父节点
当前提交
c4d249666e

二进制
AdMob/.vs/AdMob/CopilotIndices/17.14.827.52834/CodeChunks.db


二进制
AdMob/.vs/AdMob/CopilotIndices/17.14.827.52834/SemanticSymbols.db


二进制
AdMob/.vs/AdMob/DesignTimeBuild/.dtbcache.v2


二进制
AdMob/.vs/AdMob/FileContentIndex/7645fd54-122d-4564-b64e-2a9ff1405703.vsidx


二进制
AdMob/.vs/AdMob/v17/.futdcache.v2


+ 31 - 0
AdMob/.vs/AdMob/v17/DocumentLayout.backup.json

@@ -0,0 +1,31 @@
+{
+  "Version": 1,
+  "WorkspaceRootPath": "C:\\Users\\savag\\source\\repos\\CartBlanche\\MonoGame\\MonoGame-Samples\\AdMob\\",
+  "Documents": [],
+  "DocumentGroupContainers": [
+    {
+      "Orientation": 0,
+      "VerticalTabListWidth": 256,
+      "DocumentGroups": [
+        {
+          "DockedWidth": 200,
+          "SelectedChildIndex": -1,
+          "Children": [
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{e506b91c-c606-466a-90a9-123d1d1e12b3}"
+            },
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
+            },
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{aa2115a1-9712-457b-9047-dbb71ca2cdd2}"
+            }
+          ]
+        }
+      ]
+    }
+  ]
+}

+ 31 - 0
AdMob/.vs/AdMob/v17/DocumentLayout.json

@@ -0,0 +1,31 @@
+{
+  "Version": 1,
+  "WorkspaceRootPath": "C:\\Users\\savag\\source\\repos\\CartBlanche\\MonoGame\\MonoGame-Samples\\AdMob\\",
+  "Documents": [],
+  "DocumentGroupContainers": [
+    {
+      "Orientation": 0,
+      "VerticalTabListWidth": 256,
+      "DocumentGroups": [
+        {
+          "DockedWidth": 200,
+          "SelectedChildIndex": -1,
+          "Children": [
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{e506b91c-c606-466a-90a9-123d1d1e12b3}"
+            },
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
+            },
+            {
+              "$type": "Bookmark",
+              "Name": "ST:0:0:{aa2115a1-9712-457b-9047-dbb71ca2cdd2}"
+            }
+          ]
+        }
+      ]
+    }
+  ]
+}

二进制
AdMob/.vs/ProjectEvaluation/admob.metadata.v9.bin


二进制
AdMob/.vs/ProjectEvaluation/admob.projects.v9.bin


二进制
AdMob/.vs/ProjectEvaluation/admob.strings.v9.bin


+ 4 - 3
AdMob/.vscode/launch.json

@@ -2,16 +2,17 @@
     "version": "0.2.0",
     "version": "0.2.0",
     "configurations": [
     "configurations": [
         {
         {
-            "name": "MonoGame Android Build & Run",
+            "name": "MonoGame Android Build, Deploy & Run",
             "type": "coreclr",
             "type": "coreclr",
             "request": "launch",
             "request": "launch",
             "program": "dotnet",
             "program": "dotnet",
             "args": [
             "args": [
-                "build", "AdMob.Android.csproj"
+                "publish", "AdMob.Android.csproj", "-c", "Debug", "-f", "net8.0-android"
             ],
             ],
             "cwd": "${workspaceFolder}",
             "cwd": "${workspaceFolder}",
             "console": "integratedTerminal",
             "console": "integratedTerminal",
-            "stopAtEntry": false
+            "stopAtEntry": false,
+            "postDebugTask": "Install and Run APK"
         }
         }
     ]
     ]
 }
 }

+ 11 - 0
AdMob/.vscode/tasks.json

@@ -0,0 +1,11 @@
+{
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "Install and Run APK",
+            "type": "shell",
+            "command": "adb install -r bin/Debug/net8.0-android/com.companyname.admob.apk && adb shell monkey -p com.companyname.admob -c android.intent.category.LAUNCHER 1",
+            "problemMatcher": []
+        }
+    ]
+}

+ 40 - 5
AdMob/Activity1.cs

@@ -7,14 +7,18 @@ using Android.Views;
 using Android.Widget;
 using Android.Widget;
 using Android.OS;
 using Android.OS;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
-using AdSense;
+using Android.Gms.Ads;
 using Android.Content.PM;
 using Android.Content.PM;
 
 
 namespace MonoGame.Samples.AdMob
 namespace MonoGame.Samples.AdMob
 {
 {
-	[Activity (Label = "MonoGame.Samples.AdMob", MainLauncher = true
-	          , Icon = "@drawable/icon", Theme = "@style/Theme.Splash",ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)]	
-	public class Activity1 : AndroidGameActivity
+    [Activity (
+        Label = "AdMob",
+        MainLauncher = true,
+        Icon = "@drawable/icon",
+        Theme = "@style/Theme.Splash",
+        ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)]	
+    public class Activity1 : AndroidGameActivity
     {
     {
         private Game1 _game;
         private Game1 _game;
         private View _view;
         private View _view;
@@ -23,10 +27,41 @@ namespace MonoGame.Samples.AdMob
         {
         {
             base.OnCreate(bundle);
             base.OnCreate(bundle);
 
 
+            // Initialize Google Mobile Ads SDK
+            MobileAds.Initialize(this);
+
+            // Create the MonoGame view
             _game = new Game1();
             _game = new Game1();
             _view = _game.Services.GetService(typeof(View)) as View;
             _view = _game.Services.GetService(typeof(View)) as View;
 
 
-            SetContentView(_view);
+            // Create a layout to hold both the game and the ad
+            var layout = new LinearLayout(this)
+            {
+                Orientation = Orientation.Vertical
+            };
+
+            // Add the MonoGame view
+            layout.AddView(_view, new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.MatchParent,
+                0, 1f)); // Weight 1 to fill remaining space
+
+            // Create and add the AdMob banner
+            var adView = new AdView(this)
+            {
+                AdSize = AdSize.Banner,
+                AdUnitId = "ca-app-pub-3940256099942544/6300978111" // Test Ad Unit ID
+            };
+            var adParams = new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.MatchParent,
+                ViewGroup.LayoutParams.WrapContent);
+            layout.AddView(adView, adParams);
+
+            // Load an ad
+            var adRequest = new AdRequest.Builder().Build();
+            adView.LoadAd(adRequest);
+
+            // Set the layout as the content view
+            SetContentView(layout);
             _game.Run();
             _game.Run();
         }
         }
     }
     }

+ 1 - 2
AdMob/AdMob.Android.csproj

@@ -32,8 +32,7 @@
 
 
   <ItemGroup>
   <ItemGroup>
     <PackageReference Include="MonoGame.Framework.Android" Version="3.8.*" />
     <PackageReference Include="MonoGame.Framework.Android" Version="3.8.*" />
-    <AndroidJavaSource Include="AdMobHelper.java" />
-    <AndroidJavaLibrary Include="GoogleAdMobAdsSdk-4.3.1.jar" />
+    <PackageReference Include="Xamarin.GooglePlayServices.Ads" Version="120.0.0" />
   </ItemGroup>
   </ItemGroup>
 
 
 </Project>
 </Project>

+ 0 - 40
AdMob/AdMobHelper.cs

@@ -1,40 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-using Android.App;
-using Android.Content;
-using Android.Runtime;
-using Android.Views;
-using Android.Widget;
-using Android.OS;
-
-namespace AdSense
-{
-	public static class AdMobHelper
-	{
-		private static IntPtr _helperClass = JNIEnv.FindClass("MonoGame/AdMobHelper");
-			
-		public static void AddTestDevice(View view,string deviceid)
-		{
-			var s = new Java.Lang.String(deviceid);	
-			IntPtr methodId = JNIEnv.GetStaticMethodID(_helperClass, "addTestDevice", "(Landroid/view/View;Ljava/lang/String;)V");
-			JNIEnv.CallStaticVoidMethod(_helperClass, methodId, new JValue[2] { new JValue(view), new JValue(s) });
-		}
-		
-		public static View CreateAdView(Activity context, string id)
-		{				
-			var s = new Java.Lang.String(id);			
-			IntPtr methodId = JNIEnv.GetStaticMethodID(_helperClass, "createAdView", "(Landroid/app/Activity;Ljava/lang/String;)Landroid/view/View;");
-			IntPtr view = JNIEnv.CallStaticObjectMethod(_helperClass, methodId, new JValue[2] { new JValue(context), new JValue(s) });
-			return new Java.Lang.Object(view, JniHandleOwnership.TransferLocalRef).JavaCast<View>();
-		}
-		
-		public static void RequestFreshAd(View view)
-		{
-			IntPtr methodId = JNIEnv.GetStaticMethodID(_helperClass, "requestFreshAd", "(Landroid/view/View;)V");
-			JNIEnv.CallStaticVoidMethod(_helperClass, methodId, new JValue(view));
-		}
-	}
-}
-
-

+ 0 - 33
AdMob/AdMobHelper.java

@@ -1,33 +0,0 @@
-package MonoGame;
-
-import android.view.View;
-import android.app.Activity;
-import com.google.ads.AdView;
-import com.google.ads.AdRequest;
-import com.google.ads.AdSize;
-import java.lang.String;
-
-public class AdMobHelper
-{
-  private AdMobHelper() { }
-  
-  public static void addTestDevice(View view,String deviceid)
-  {
-    AdRequest request = new AdRequest();
-    request.addTestDevice(AdRequest.TEST_EMULATOR);
-    request.addTestDevice(deviceid); 
-    ((AdView)view).loadAd(request);  
-  }
-
-  public static View createAdView(Activity activity, String appid)
-  {
-    // Create the adView
-    AdView view = new AdView(activity, AdSize.BANNER, appid);    
-    return view;
-  }
-
-  public static void requestFreshAd(View view)
-  {
-    ((AdView)view).loadAd(new AdRequest());
-  }
-}

二进制
AdMob/GoogleAdMobAdsSdk-4.3.1.jar


+ 3 - 4
AdMob/Properties/AndroidManifest.xml

@@ -1,9 +1,8 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="MonoGame.Samples.AdMob">
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="MonoGame.Samples.AdMob">
-	<application android:label="MonoGame.Samples.AdMob" android:icon="@drawable/icon">
-		 <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
+	<application android:label="AdMob" android:icon="@drawable/icon">
+		<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
 	</application>
 	</application>
-	<uses-sdk />
 	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 	<uses-permission android:name="android.permission.INTERNET" />
 	<uses-permission android:name="android.permission.INTERNET" />
 </manifest>
 </manifest>