admob之从服务上下文 Android 显示 Admob 广告
你猜
阅读:57
2024-10-25 08:56:14
评论:0
我想展示来自正在运行的服务的广告。我的服务检查剪贴板中的文本,并显示一个带有信息的弹出窗口(字典服务)。但是当我调试应用程序时,它显示错误:
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。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。