Android和cocos2d-x 中Amazon添加廣告

來源:互聯網
上載者:User

標籤:android   style   c   class   blog   code   

首先到Amazon的官網下載APP-SDK,之後解壓就可以,不過這個裡面有很多的sdk,我們只用匯入廣告的adk就行,這個要比google的簡單,直接加入jar的包就行。在這裡附上amazon SDK的下載路徑:

1 https://developer.amazon.com/public/resources/development-tools/sdk

解壓adk後找到jar包,copy到android工程的libs目錄就可以,如lib目錄如下如所示:

接著我們就需要修改Android的代碼了,其實還是老一套修改資訊清單檔,接著寫java代碼就行:

加入所需的許可權:

1     <uses-permission android:name="android.permission.INTERNET" />2     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />3     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />4     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />5     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

聲明Activity

1         <activity2      android:name="com.amazon.device.ads.AdActivity"3      android:configChanges="keyboardHidden|orientation|screenSize">4      </activity>

接著看看Activity的實現:

 1 /** 2  * Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3  * Licensed under the Apache License, Version 2.0 (the "License"). 4  * You may not use this file except in compliance with the License. 5  * A copy of the License is located at http://aws.amazon.com/apache2.0/ 6  * or in the "license" file accompanying this file. 7  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 8  * CONDITIONS OF ANY KIND, either express or implied.  9  * See the License for the specific language governing permissions and10  * limitations under the License.11  */12 package com.lemi.testamazon;13 14 import com.amazon.device.ads.*;15 16 import android.app.Activity;17 import android.os.Bundle;18 import android.util.Log;19 import android.view.LayoutInflater;20 import android.view.View;21 import android.view.ViewGroup.LayoutParams;22 23 24 public class MainActivity extends Activity {25     private AdLayout adView; 26    27     private static final String APP_KEY = "sample-app-v1_pub-2";28     private static final String LOG_TAG = "lemi"; 29 30     @Override31     public void onCreate(final Bundle savedInstanceState) {32         super.onCreate(savedInstanceState);33         AdRegistration.enableLogging(true);      34         LayoutInflater inflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);35         View view = inflater.inflate(R.layout.main, null);36         37         this.adView = (AdLayout) view.findViewById(R.id.ad_view);38         this.adView.setListener(new SampleAdListener());39         40         addContentView(view, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));41         42         try {43             AdRegistration.setAppKey(APP_KEY);44         } catch (final Exception e) {45             Log.e(LOG_TAG, "Exception thrown: " + e.toString());46             return;47         }48 49         loadAd();50         51     }52     53 54     public void loadAd() {55         this.adView.loadAd();56     }57     58     class SampleAdListener extends DefaultAdListener59     {60 61         @Override62         public void onAdLoaded(final Ad ad, final AdProperties adProperties) {63             Log.i(LOG_TAG, adProperties.getAdType().toString() + " ad loaded successfully.");64         }65 66         @Override67         public void onAdFailedToLoad(final Ad ad, final AdError error) {68             Log.w(LOG_TAG, "Ad failed to load. Code: " + error.getCode() + ", Message: " + error.getMessage());69         }70     71         @Override72         public void onAdExpanded(final Ad ad) {73             Log.i(LOG_TAG, "Ad expanded.");74            75         }76         77         @Override78         public void onAdCollapsed(final Ad ad) {79             Log.i(LOG_TAG, "Ad collapsed.");80            81         }82     }83 }

需要注意的是我在這裡使用的Amazon的測試介面,如果你是自己開發的應用,你還是要換上自己申請的KEY值

1 private static final String APP_KEY = "sample-app-v1_pub-2";

上一張吧:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.