3

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

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();
}

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

4

2 回答 2

0

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

于 2013-01-14T13:32:35.820 回答
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;
}}

首先,我在没有处理程序的情况下运行代码并且没有加载广告,所以我给了一些时间先加载广告。

希望这会有所帮助。

于 2017-05-21T04:56:03.633 回答