Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Android™ Development

Reply
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Impements View

OK - this is complicated and off topic but this is the best place I can think of to to find the answer [Migration away from BB for a released BB project]

 

This stumps me and is important as we move closer to the Cordova Merge

 

It's not obvious what this does - so I'll explain

 

It sets up admob, ties into analytics, a callback system and finishes off with procurement

 

The View never gets created for admimage in the R,i.main so I and up with sod all which makes a null assignment kill me

 

The think to look at below is the imageView

 

Please bear in mind that my knowledge of Java hits around 2%

 

If I get this finished its a great tool for others

 

/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.peardox.vexed;

import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;

import org.apache.cordova.*;
import android.util.Log;

public class vexed extends DroidGap implements AdListener {
	private static final String AdMob_Ad_Unit = "a15156dd247f92b";
	private Handler mHandler = new Handler();
	private AdView adView;
	private ImageView imageView;
	private boolean firstAdReceived = false;
	private static final String TAG = "Simon";
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        String adspref = "";
        adspref = prefs.getString("showads", "unset");
        
        Log.i(TAG, "showads : " + adspref);

        if(adspref.equals("unset")) {
        	adspref = "yes";
            Editor editor = prefs.edit();
            editor.putString("showads", adspref);
            if(editor.commit()) {
                Log.i(TAG, "Wrote initial showads : " + adspref);
            }
            else {
                Log.e(TAG, "Write showads failed");
            }
        }
        
        
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
                
  //      setContentView(R.layout.main);
        
        if(adspref.equals("yes")) {
            Log.i(TAG, "Start AdMob");
	        mHandler.postDelayed(new Runnable() {
	            public void run() {
	                doAdMob();
	            }
	        }, 5000);
        } else {
            Log.i(TAG, "No Ads");
        }
        
        Log.i(TAG, "Eyelid : " + R.id.banimage );

        
/*
        adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit); 
        LinearLayout layout = super.root;
        layout.addView(adView);
        AdRequest request = new AdRequest();
        request.setTesting(true);
        adView.loadAd(request);         
*/
        
    }
    
    private void doAdMob() {
        // Create the adView
        adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
        adView.setAdListener(this);
        // Lookup your LinearLayout - get the super.root
        LinearLayout layout = super.root;

//        LinearLayout layout = (LinearLayout) findViewById(R.id.adLayout);
        // Add the adView to it
        layout.addView(adView);
        // This centers the ads in landscape mode.        
        layout.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL);
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        // and finally...     
        request.addTestDevice("F5053355D8094775DC4BEA1CC2064075");
        adView.loadAd(request);     
        Log.i(TAG, "Eyelid : " + R.id.banimage );
        imageView = (ImageView) findViewById(R.id.banimage);
//        imageView.setOnClickListener(this);
    }
    
    @Override
    public void onReceiveAd(Ad ad) {
        // TODO Auto-generated method stub              
        Log.i(TAG, "AdEvent : onReceiveAd");
        firstAdReceived = true;
        // Hide the custom image and show the AdView.
//        imageView.setVisibility(View.GONE);
//      adView.setVisibility(View.VISIBLE);
        if(imageView == null) {
            Log.i(TAG, "AdEvent : onFailedToReceiveAd - NO IMAGE!");
        }
    }           
    
    @Override
    public void onPresentScreen(Ad ad) {
        // TODO Auto-generated method stub
        Log.i(TAG, "AdEvent : onPresentScreen");
    }
    
    @Override
    public void onLeaveApplication(Ad ad) {
        // TODO Auto-generated method stub
        Log.i(TAG, "AdEvent : onLeaveApplication");
    }
    
    @Override
    public void onFailedToReceiveAd(Ad ad, ErrorCode code) {
        // TODO Auto-generated method stub
        Log.i(TAG, "AdEvent : onFailedToReceiveAd");
        if(imageView == null) {
            Log.i(TAG, "AdEvent : onFailedToReceiveAd - NO IMAGE!");
        } else {
        	adView.setVisibility(View.GONE);
//        	imageView.setVisibility(View.VISIBLE);
        }
    }
    
    @Override
    public void onDismissScreen(Ad ad) {
        // TODO Auto-generated method stub
        Log.i(TAG, "AdEvent : ");
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        com.google.analytics.tracking.android.EasyTracker.getInstance().activityStop(this);
    }
    
}

 

 




Click the like button if you find my posts useful!