Blog

  • Flurry SDK Phonegap for Android

    This tutorial will be helpful to those who want to integrate Flurry sdk within android and are struggling to use the same within phonegap.

    In brief Flurry is a mobile analytics and monetization platform.

    In order to make use of Flurry sdk you have firstly signup on their site.

    https://login.flurry.com/signup


    Just fill in basic details and, you will get an Api Key, which is required to make the project run.

    In this project we not only need the key but also a adspace.

    For that you will be required to login and go to publisher tab and register an adspace.

    http://support.flurry.com/index.php?title=Guides/f/Publishers/Code/MediatedBanners/Android
    Firstly add the flurry sdk , cordova jar files in libs folder , add to build path.

    In plugin.js (assets ->www) add

     

     

    	window.showAdd = function(str, callback) {
    
    	    cordova.exec(callback, function(err) {
    
    	        callback('Nothing to echo.');
    
    	    }, "FlurryPlugin", "showAD", [str]);
    
    	};

    It shows that FlurryPlugin is our service name, showAD is action method.

    Here is plugin file,

     

    	public class FlurryPlugin extends CordovaPlugin {
    
    	static FrameLayout adLayout;
    
    	private static String adSpace;
    
    	private static Context mContext;
    
    	private static FlurryAdSize size;
    
    	Handler handler;
    
    	
    
    	@Override
    
    	public boolean execute(String action, JSONArray args,
    
    	CallbackContext callbackContext) throws JSONException {
    
    	if (action.equals("showAD")) {  // check action name as specified in plugin.js
    
    	
    
    	new FlurryAdActivity().fetchAd(mContext, adSpace, adLayout,
    
    	size);// call flurry ad method to fetch ad.
    
    	        }
    
    	        return false; // Returning false results in a "MethodNotFound" error.
    
    	    }
    
    	
    
    	    
    
    	    // set flurry data.
    
    	    
    
    	    public void setFlurryData(Context context, String ad_space, FrameLayout ad_Layout,
    
    	            FlurryAdSize fsize) {
    
    	        mContext = context;
    
    	        adSpace = ad_space;
    
    	        adLayout = ad_Layout;        
    
    	        size = fsize;
    
    	        
    
    	
    
    	    }
    
    	
    
    	}

    and here is the Activity file in which we load the html layout as well as android xml.

     

    	public class FlurryAdActivity extends Activity implements CordovaInterface,
    
    	FlurryAdListener {
    
            CordovaWebView cwv;
    
    	FrameLayout adLayout;
    
    	private final String Tag = "FlurryAdServingAPI";
    
    	public static String apiKey;
    
    	private String adSpace;
    
    	private Context mContext;
    
    	FlurryAdSize size = FlurryAdSize.BANNER_BOTTOM;
    
    	Handler handler;
    
    	private final ExecutorService threadPool = Executors.newCachedThreadPool();
    
    	
    
    	@Override
    
    	protected void onCreate(Bundle savedInstanceState) {
    
    	super.onCreate(savedInstanceState);
    
    	handler = new Handler();
    
    	        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
    	        setContentView(R.layout.activity_main);
    
    	        cwv = (CordovaWebView) findViewById(R.id.tutorialView);
    
    	        Config.init(this);
    
    	        cwv.loadUrl(Config.getStartUrl());
    
    	        adLayout = (FrameLayout) findViewById(R.id.adLayout);
    
    	        mContext = FlurryAdActivity.this;
    
    	        apiKey = getResources().getString(R.string.flurry_api_key);
    
    	        adSpace = getResources().getString(R.string.adSpaceName);
    
    	
    
    	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    
    	                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public Activity getActivity() {
    
    	        // TODO Auto-generated method stub
    
    	        return this;
    
    	    }
    
    	
    
    	    @Override
    
    	    public ExecutorService getThreadPool() {
    
    	        return threadPool;
    
    	    }
    
    	
    
    	    @Override
    
    	    public Object onMessage(String arg0, Object arg1) {
    
    	        // TODO Auto-generated method stub
    
    	        return null;
    
    	    }
    
    	
    
    	    @Override
    
    	    public void setActivityResultCallback(CordovaPlugin arg0) {
    
    	        // TODO Auto-generated method stub
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public void startActivityForResult(CordovaPlugin arg0, Intent arg1, int arg2) {
    
    	        // TODO Auto-generated method stub
    
    	
    
    	    }
    
    	
    
    	    public void fetchAd(final Context context, final String ad_space,
    
    	            final FrameLayout ad_Layout, final FlurryAdSize fsize) {
    
    	        Log.i("In fetchAd", "method");
    
    	
    
    	
    
    	                FlurryAds.fetchAd(context, ad_space, ad_Layout, fsize);
    
    	
    
    	
    
    	    }
    
    	
    
    	    // register Ad Listner for flurry , send data to AlertBack class for context.
    
    	    
    
    	    @Override
    
    	    public void onResume() {
    
    	        super.onResume();
    
    	        try {
    
    	            Log.d(Tag, "in resume");
    
    	            FlurryAds.setAdListener(this);
    
    	            FlurryAgent.setLogEnabled(true);
    
    	            FlurryAgent.setLogLevel(2);
    
    	            FlurryAgent.onStartSession(mContext, apiKey);
    
    	            FlurryAds.initializeAds(mContext);
    
    	
    
    	                    new FlurryPlugin().setFlurryData(mContext, adSpace, adLayout,
    
    	                            size);
    
    	
    
    	            FlurryAds.enableTestAds(false);
    
    	
    
    	        } catch (Exception e) {
    
    	            Log.e(Tag, e.getMessage());
    
    	        }
    
    	    }
    
    	
    
    	    @Override
    
    	    public void  (final String adSpace) {
    
    	        // called when the ad has been prepared, ad can be displayed:
    
    	        Log.d(Tag, "spaceDidReceiveAd( " + adSpace + " )");
    
    	
    
    	        final Runnable runnable = new Runnable() {
    
    	
    
    	            public void run() {
    
    	
    
    	                FlurryAds.displayAd(mContext, adSpace, adLayout);
    
    	                Toast toast = Toast.makeText(mContext, "Displaying Ad",
    
    	                        Toast.LENGTH_SHORT);
    
    	                toast.show();
    
    	            }
    
    	        };
    
    	        handler.postDelayed(runnable, 900);
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onStop() {
    
    	        super.onStop();
    
    	        FlurryAds.removeAd(mContext, adSpace, adLayout);
    
    	        FlurryAgent.onEndSession(mContext);
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onAdClicked(String arg0) {
    
    	        Log.d(Tag, "onAdClicked( " + arg0 + " )");
    
    	        Toast toast = Toast.makeText(mContext, "onAdClicked",
    
    	                Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onAdClosed(String arg0) {
    
    	        Log.d(Tag, "onAdClosed( " + arg0 + " )");
    
    	        Toast toast = Toast
    
    	                .makeText(mContext, "onAdClosed", Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onAdOpened(String arg0) {
    
    	        Log.d(Tag, "onAdOpened( " + arg0 + " )");
    
    	        Toast toast = Toast
    
    	                .makeText(mContext, "onAdOpened", Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onApplicationExit(String arg0) {
    
    	        Log.d(Tag, "onApplicationExit( " + arg0 + " )");
    
    	        Toast toast = Toast.makeText(mContext, "onApplicationExit",
    
    	                Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onRenderFailed(String arg0) {
    
    	        Log.d(Tag, "onRenderFailed( " + arg0 + " )");
    
    	        Toast toast = Toast.makeText(mContext, "onREnderFailed",
    
    	                Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	    }
    
    	
    
    	    @Override
    
    	    public void onVideoCompleted(String arg0) {
    
    	        Log.d(Tag, "onVideoCompleted( " + arg0 + " )");
    
    	        Toast toast = Toast.makeText(mContext, "onVideoCompleted",
    
    	                Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	
    
    	    }
    
    	
    
    	    @Override
    
    	    public boolean shouldDisplayAd(String arg0, FlurryAdType arg1) {
    
    	        Log.d(Tag, "shouldDisplayAd( " + arg0 + ", " + arg1 + " )");
    
    	        return true;
    
    	    }
    
    	
    
    	    @Override
    
    	    public void spaceDidFailToReceiveAd(String arg0) {
    
    	        Log.d(Tag, "spaceDidFailToReceiveAd(" + arg0 + " )");
    
    	        Toast toast = Toast.makeText(mContext, "spaceDidFailToReceiveAd",
    
    	                Toast.LENGTH_SHORT);
    
    	        toast.show();
    
    	    }
    
    	}

    In onResume method we start the flurry session by passing the key to it, initialize the ad.

    Also I call setFlurryData method in FlurryPlugin to set the data related to flurry and context.

    and when execute method gets called (by clicking on button) the fetchAd method in our activity gets called which sends request to server ,
    finally ad gets displayed by this method spaceDidReceiveAd or error message by this method spaceDidFailToReceiveAd.

    As I have explained in previous tutorial http://oodlestechnologies.com/blogs/Writing-Phonegap-plugin-for-Android

    execute method is link between javascript, native code.

    In our Activity file we are extending CordovaInterface so that we can incorporate cordova webview in android view.

    Here is the layout file.

     

    	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
    	 xmlns:tools="http://schemas.android.com/tools"
    
    	 android:layout_width="match_parent"
    
    	 android:layout_height="match_parent"
    
    	
    
    	 tools:context=".FlurryAdActivity" >
    
    	
    
    	 <FrameLayout
    
    	 android:id="@+id/adLayout"
    
    	        android:layout_width="wrap_content"
    
    	        android:layout_height="wrap_content"
    
    	        android:background="#887766"
    
    	         >
    
    	    </FrameLayout>
    
    	
    
    	<org.apache.cordova.CordovaWebView
    
    	    android:id="@+id/tutorialView"
    
    	    android:layout_width="match_parent"
    
    	    android:layout_height="match_parent"
    
    	    android:layout_below="@+id/adLayout"/>
    
    	    
    
    	</RelativeLayout>



    As soon as you hit the button you will see the ad displayed toast and, ad will be displayed just above the webview.

    Also before I forget add the following to manifest file

     

    	 <activity
    
    	            android:name="com.flurry.android.FlurryFullscreenTakeoverActivity"
    
    	            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
    
    	        </activity>

     

    Here is the output

     

     

    You may download code from here

     

    Download Code

    Thanks,

    Ravi Sharma

    ravi.sharma@oodlestechnologies.com

Tags: android

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com