我想展示正在投放的服务中的广告。我的服务检查剪贴板的文本,并显示一个包含信息的弹出窗口(字典服务)。但是当我调试应用程序时,它显示错误:
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);
OnCreate中:
@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();
}
Sory因为我不能在这里发布完整的代码。有服务检查剪贴板,然后在视图布局中显示内容。
答案 0 :(得分:0)
您可以在弹出窗口中显示广告(我猜您是指从您的服务开始的活动)。您得到的错误是因为您使用不是活动的Context对象创建AdView。传入作为弹出窗口的Activity。
答案 1 :(得分:0)
无法修改您的代码但是,我们可以通过服务展示Admob广告。以下是我在InterstitialAd应用程序中使用的代码:
public class ServiceAd extends Service {
private InterstitialAd mInterstitialAd;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mInterstitialAd = new InterstitialAd(getApplicationContext());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
Log.d("tag", "on closed ad");
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("tag", "The interstitial wasn't loaded yet.");
}
}
}, 5000); // 5 mins
return START_STICKY;
}}
首先,我运行没有处理程序的代码,广告未加载,所以我花了一些时间来加载广告。
希望这会有所帮助。