gpt4 book ai didi

admob - 从服务上下文 Android 显示 Admob 广告

转载 作者:行者123 更新时间:2023-12-01 11:35:40 26 4
gpt4 key购买 nike

我想展示来自正在运行的服务的广告。我的服务检查剪贴板中的文本,并显示一个带有信息的弹出窗口(字典服务)。但是当我调试应用程序时,它显示错误:

Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity.

有人说没有事件上下文就无法启动。有什么解决办法吗?

我更新了下面的代码:显示弹出窗口的代码:

    private void showCaptureWindow() {

setTextToKeywordEdit(mClipboardText);

makeDictContent(mClipboardText);

if(!mDictContentView.hasFocus())
mDictContentView.requestFocus();

if(false == bHasAddedView)
{
int bgColor = MultiDictUtils.GetBgColor();
int textColor = MultiDictUtils.GetTextColor();
mParentViewLayout.setBackgroundColor(bgColor);
mDictContentView.setBackgroundColor(bgColor);
mLineView0.setBackgroundColor(textColor);
mLineView1.setBackgroundColor(textColor);
mLineView2.setBackgroundColor(textColor);
mLineView3.setBackgroundColor(textColor);
mLineView4.setBackgroundColor(textColor);

updateDisplaySize(true);

mWindowParams.x = mWindow_Default_X;
mWindowParams.y = mWindow_Default_Y;

mWinStatus = 0;
mWindowResizeBtn.setImageResource(R.drawable.ic_btn_max_win);
// Look up the AdView as a resource and load a request.
adView = (AdView)mCaptureWindowLayout.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
mWindowManager.addView(mCaptureWindowLayout, mWindowParams);
bHasAddedView = true;
}
}

初始化服务:

    private void initService() {
MyLog.v(TAG, "initService()");

mClipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

if(mClipboardManager.hasText())
{
mClipboardText = mClipboardManager.getText().toString();
}

LayoutInflater factory = LayoutInflater.from(this);
mCaptureWindowLayout = (LinearLayout)factory.inflate(R.layout.capture_window, null);

mWindowCloseBtn = (ImageButton)mCaptureWindowLayout.findViewById(R.id.windowCloseBtn);
mWindowResizeBtn = (ImageButton)mCaptureWindowLayout.findViewById(R.id.windowResizeBtn);
mWindowMoveBtn = (ImageButton)mCaptureWindowLayout.findViewById(R.id.windowMoveBtn);
mWindowSchBtn = (ImageButton)mCaptureWindowLayout.findViewById(R.id.windowSchBtn);

mKeywordEdit = (EditText)mCaptureWindowLayout.findViewById(R.id.keywordTxt);

mParentViewLayout = (LinearLayout) mCaptureWindowLayout.findViewById(R.id.parentView);
mLineView0 = (View) mCaptureWindowLayout.findViewById(R.id.lineView0);
mLineView1 = (View) mCaptureWindowLayout.findViewById(R.id.lineView1);
mLineView2 = (View) mCaptureWindowLayout.findViewById(R.id.lineView2);
mLineView3 = (View) mCaptureWindowLayout.findViewById(R.id.lineView3);
mLineView4 = (View) mCaptureWindowLayout.findViewById(R.id.lineView4);
mDictContentView = (WebView)mCaptureWindowLayout.findViewById(R.id.dictContentWindow);
mDictContentView.setWebViewClient(new DictWebViewClient(new ServiceWebViewClientCallback()));

WebSettings webSettings = mDictContentView.getSettings();
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);

创建:

    @Override
public void onCreate() {

mMultiDictUtils = new MultiDictUtils(this);

mHandler = new Handler();

mClipboardTask = new Runnable() {
public void run() {

clipboardCheck();

mHandler.postDelayed(mClipboardTask, CLIPBOARD_TIMER);
}
};

Runnable initServiceTask = new Runnable() {
public void run() {

initService();

mHandler.postDelayed(mClipboardTask, CLIPBOARD_TIMER);
}
};

mHandler.postDelayed(initServiceTask, 5000);

super.onCreate();
}

对不起,因为我不能在这里发布完整的代码。有服务检查剪贴板,然后在 View 布局中显示内容。

最佳答案

您可以在弹出窗口中显示一个广告(我假设您指的是您从服务启动的事件)。您收到的错误是因为您正在使用不是 Activity 的 Context 对象创建 AdView。传入作为弹出窗口的 Activity。

关于admob - 从服务上下文 Android 显示 Admob 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14313641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com