Pages

Subscribe:

Ads 468x60px

Labels

顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2013年5月23日 星期四

Google AdMob 廣告 Android 版橫幅廣告 XML



總覽

AdView 除了可以用 Java 撰寫外,只使用 XML 也可以達到相同目的,方法如下:
  • 將 SDK 導入應用程式
  • 在 res/layout/main.xml 中定義 com.google.ads.AdView,並用 ads:loadAdOnCreate 屬性指定它立即載入廣告。
  • 您也可以不要強制要求 AdView 立即載入廣告,然後改在執行期間查詢 AdView 這項資源,並要求它請求廣告。

定義 com.google.ads.AdView

導入廣告最簡單的方式是定義 AdView,方法和定義 res/layout/main.xml 的任何部分一樣:

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
   android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"
                         ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                         ads:loadAdOnCreate="true"/>
在這裡,您也必須用 AdMob 發佈商編號來取代 MY_AD_UNIT_ID,並在 ads:testDevices 屬性中加入自己的裝置編號,才能在裝置上取得測試廣告。請注意,這裡在指定 adUnitId 和 adSize 時加入了 ads 命名空間參照。在 Android 版面引擎建立 AdView 後,這段程式碼就會立即嘗試載入廣告。

查詢及載入

如果您想要控制在應用程式中用來載入廣告的 AdRequest,可以從上述程式碼中移除 ads:loadAdOnCreate="true" 這一行,然後改為透過 findViewById 查詢 AdView 這項資源,並指示它執行 loadAd
import com.google.ads.*;
public class BannerExample extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 查詢 AdView 這項資源,並載入請求。
    AdView adView = (AdView)this.findViewById(R.id.adView);
    adView.loadAd(new AdRequest());
  }
}
您也可以在這裡下載包含此程式碼的範例專案。






https://developers.google.com/mobile-ads-sdk/docs/android/banner_xml?hl=zh-tw

Android APP 加入 AdMob 廣告補充說明

關於 Android APP 加入 AdMob 廣告,基本上參考官網上的說明文件可以獲得絕大部分的資訊,不過我依步驟操作完後不是編譯不過紅字一堆、就是編譯成功但程式一執行就會馬上出錯跳出或是程式當掉 ....

奮戰幾個小時候終於是搞定了,圖片中上面是 AdMob 所顯示的廣告,下面則是原本以 WebView 顯示寄放在 App Engine 上面的 AdSense 廣告,這是為了要搞定掛上 AdMob 廣告所以直接拿已經完成的 Project 來直接修改測試,可別沒事真的把 APP 掛上兩種廣告來源上架,這很有可能會造成違規而被停權喔 ...


http://alvinhyc.blogspot.tw/2012/07/android-app-admob.html

2013年5月20日 星期一

Google Maps Android API V2使用及問題解決


因為Google Maps的API版本更新,之前的一些教程都是關於舊版本V1的,雖然Google說繼續提供服務,但是不再提供API Key的申請。

而新的V2版本貌似改動還挺大。也沒搜到國內有什麼系統介紹的博客文章之類的(書肯定是來不及那麼新了)。

斷斷續續折騰了大概半個月,因為對Android也不是特別熟悉,所以碰到這樣那樣的問題。

終於在昨天看見模擬器上跑的地圖了。太感人了。

下面就主要說說要成功做成這一件事的流程吧。

因為肯定有時效性,所以打上一個時間戳記:版本更新事件發生在2012年12月,而這篇博文目前的時間是2013年1月1日。

背景
一些相關的連結:

Google Maps Android API V1的介紹:

HTTPs://developers.google.com/maps/documentation/android/v1/mapkey?hl=zh-CN

Google Maps Android API v2的初步介紹:

HTTPs://developers.google.com/maps/documentation/android/

Introduction
HTTPs://developers.google.com/maps/documentation/android/intro

Getting Started
(本部分參考HTTPs://developers.google.com/maps/documentation/android/start)

1.首先安裝Google Play services SDK
Google Maps Android API是作為這個SDK的一部分發行的。

這個安裝是通過Android SDK Manager進行,配置好之後的Eclipse上面應該有Android SDK Manager的圖示,一般的SDK版本安裝和更新都在這裡進行。

安裝和更新Extras下的Google Play services即可。

2.獲取API key
獲取Maps API key需要兩樣東西:應用的signing certificate和它的package name。

獲取這個key之後,把它加在應用程式的AndroidManifest.xml檔裡即可。

為應用獲取一個key還是需要好幾個步驟的,下面詳細說明:

獲取數位憑證(digital certificate)資訊
數位憑證有Debug和Release兩種,下面主要說Debug的。

要獲取一個叫做SHA-1 fingerprint的東西,作為數位憑證的一個簡短代表。

這個指紋(fingerprint)是通過一個雜湊演算法得到的字串,為了得到你的證書的SHA-1 fingerprint,首先要找到你的debug keystore 檔,檔案名叫debug.keystore。

預設情況下它和虛擬機器AVD存放在一起,win7下的路徑是:C:\Users\your_user_name\.android\,也可以通過Eclipse中的Windows > Prefs > Android > Build來查看這個路徑。

然後,在cmd命令列裡運行下列命令:

keytool -list -v -keystore "C:\Users\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android就顯示一大堆東西,其中就有證書指紋:
1744520  


SHA1那一行就包含了證書的SHA-1 fingerprint,是二十段用冒號割開的數位段,每段是兩個十六進位的數。

在Google APIs Console上創建API Project
在Google APIs Console上創建專案,並且註冊Maps API。

首先,去這個網址:HTTPs://code.google.com/apis/console/
 
用Gmail的帳戶登錄,如果是第一次的話,需要創建專案,預設情況會創建一個叫做API Project的專案。

點擊左邊的Services,會在中間看到很多的APIs和Services,找到Google Maps Android API v2,然後把它設置成on,需要接受一些服務條款。

獲得API Key
在左邊的導航條中選擇API Access。

在出來的頁面中選擇Create New Android Key...就可以生成key了:
1744521  


然後在對話方塊中填入:SHA-1 指紋, 分號隔開,然後是應用的 package name.然後就會生成一個Key。

比如:
1744522  


3.把API Key加入應用程式
首先,建立虛擬裝置AVD和應用程式。

關於AVD,官方文檔並沒詳細介紹,我後面會有說明。

建立好應用程式,注意包名應該和申請key時候的包名一致。

之後修改AndroidManifest.xml檔:

3.1.在元素中加入子標籤

3.2.加入一些許可資訊

com.example.mapdemo.permission.MAPS_RECEIVE"/> 其中com.example.mapdemo換成自己的包名。


4. AndroidManifest.xml中的其他具體設置

許可設置

作為 的子項目,需要加入下列一些:

android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> OpenGL ES V2特性支援

同樣也是作為 的子項目。

5.加上地圖
首先佈局檔:

parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment"/> 然後在MainActivity.java:

MainActivity.java package com.example.mapdemo; import android.app.Activity;import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 遇到的問題和解決的方法
程式編譯錯誤,顯示找不到一些類
如圖:

1744523  

解決這個問題,首先需要把Google Play services的類庫載入進來:

在Eclipse裡面選擇:File > Import > Android > Existing Android Code Into Workspace然後點擊Next.

之後Browse..., 找到路徑下的/extras/google/google_play_services /libproject/google-play-services_lib, 然後選擇Finish。

第二步是添加對這個庫的引用:

在自己的專案上右鍵,選Properties,左邊選Android,然後在下面的Library裡面Add剛才的google-play-services_lib。

1744524  

之後程式就應該能運行了。

接著你可能會碰到下面的問題:

程式運行成功,但是顯示This app won't run unless you update Google Play services.

如圖:
1744525  


有傳言說V2不能在AVD上運行,可能Google還會對此問題進行更新。

經過搜索,這個問題已經在Stackoverflow上被討論過了,連結

所以看來在AVD上運行的問題已經被解決了。

解決的方法就是在AVD上安裝兩個包:vending.apk和gms.apk,(給一個網盤連結

並且AVD就選擇普通的API 16就行,不需要是Google APIs。我選的是Android4.1 API16.

安裝時把那兩個包放在目前的目錄,用命令列安裝:

1744526  

之後運行程式,就出地圖了:
1744527  

Android Google Map V2 Eclipse AVD 模擬器免破解執行方法


Android Google Map V2 Eclipse AVD (Android Emulator) 模擬器免破解執行方法 (原創)
How to use Eclipse AVD Run Android Google Map V2 Without Root AVD?
發現時機-在巨匠上鄭一鴻老師的Android APP課程時發現的~
Google MAP V2版 For Android 在Eclipse ADV 中無法啟動?
--如果想要找如何申請V2版KEY,可至Google查,可以查到許多善心人教學~
Google MAP V1 已經停止支援無法申請API KEY,改用Google MAP V2,
許多工程師們都無法在Eclipse 上的ADV跑V2版,
現在有許多方式可以解決,但需用第三方模擬器,或ROOT模擬器來克服,
而我在這提供一個另類的方式,
直接在Eclipse 上的ADV(Android Emulator)跑V2版,不需再裝第三方apk,免破解,直接可用~
事前準備~
先要準備好 SDK Manager 開啟,並將 Eclipse 升級到 ADT 21.1 版本,
並下載4.2.2版(API17)且需下載Google APIs
在Extras中把 Google Play Service, Google Play 相關的都裝上吧!
我是全選全裝!

2013年5月19日 星期日

Google地圖v2 - 使用標記與設定地圖鏡頭焦點

Google地圖v2 - 地名或地址轉成位置後以標記顯示在地圖上

Android App教學 - Location位置資訊應用

Google地圖v2 - 地圖上繪製連續線、多邊形與圓形

Google地圖v2 - 地圖種類與地圖UI設定

Google地圖v2 - 基本Google地圖展示

Google Maps Android API

詳見



Getting Started

Before you can begin working with the API, you will need to download the API and ensure that you have a Google Maps Android API v2 key. Both the API and the key are freely available.

Overview

Creating a new Android application that uses the Google Maps Android API v2 requires several steps. Many of the steps outlined in this section will only have to be performed once, but some of the information will be a handy reference for future applications. The overall process of adding a map to an Android application is as follows:
  1. Download and configure the Google Play services SDK. The Google Maps Android API is distributed as part of this SDK.
  2. Obtain an API key. To do this, you will need to register a project in the Google APIs Console, and get a signing certificate for your app.
  3. Specify settings in the Application Manifest.
  4. Add a map to a new or existing Android project.
  5. Publish your application!
You may wish to begin by looking at some sample code, which is included with the Google Play services SDK.

Getting the Google Maps Android API v2

The API is distributed as part of the Google Play services SDK, which you can download with the Android SDK Manager. To use the Google Maps Android API v2 in your app, you will first need to install the Google Play services SDK. To learn how to install the package, see the Google Play services documentation.
As a prerequisite, you need to install the Android SDK. To learn how to do this, see Installing the SDK.

The Google Maps API Key

Note: The Google Maps Android API v2 uses a new system of managing keys. Existing keys from a Google Maps Android v1 application, commonly known as MapView, will not work with the v2 API.
To access the Google Maps servers with the Maps API, you have to add a Maps API key to your application. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users. You obtain a Maps API key from the Google APIs Console by providing your application's signing certificate and its package name. Once you have the key, you add it to your application by adding an element to your application's manifest file AndroidManifest.xml.
Understanding the process of registering your application and obtaining a key requires some knowledge of Android's publishing process and requirements. In summary, all Android applications must be signed with a digital certificate for which you hold the private key. Because digital certificates are unique, they provide a simple way of uniquely identifying your app. This makes them useful for tracking your application in systems such as Google Play Store, and for tracking your application's use of resources such as the Google Maps servers.
Note: Refer to the Android Guide Signing Your Applications for more information regarding digital certificates.
Maps API keys are linked to specific certificate/package pairs, rather than to users or applications. You only need one key for each certificate, no matter how many users you have for an application. Applications that use the same certificate can use the same API key. However, the recommended practice is to sign each of your applications with a different certificate and get a different key for each one.
Obtaining a key for your application requires several steps. These steps are outlined here, and described in detail in the following sections.
  1. Retrieve information about your application's certificate.
  2. Register a project in the Google APIs Console and add the Maps API as a service for the project.
  3. Once you have a project set up, you can request one or more keys.
  4. Finally, you can add your key to your application and begin development.

Displaying certificate information

The Maps API key is based on a short form of your application's digital certificate, known as its SHA-1 fingerprint. The fingerprint is a unique text string generated from the commonly-used SHA-1 hashing algorithm. Because the fingerprint is itself unique, Google Maps uses it as a way to identify your application.
To display the SHA-1 fingerprint for your certificate, first ensure that you have the certificate itself. You may have two certificates:
  • Debug certificate: The Android SDK tools generate this certificate automatically when you do a "debug" build from the command line, or when you build and run a project from Eclipse without exporting it as a released application. The certificate is only for use with an application that you're testing; you can't publish an app that's signed with a debug certificate. The debug certificate is described in more detail in the section Signing in Debug Mode in the Android Developer Documentation. You can generate an API key from this certificate, but only use the key for testing, never for production.
  • Release certificate: The Android SDK tools generate this certificate when you do a "release" build with either ant program or Eclipse. You can also generate this certificate using the keytool program. This certificate can be used with an app you release to the world. Once you have the correct certificate for your needs, you can display its SHA-1 fingerprint using the keytool program.
  • For more information about Keytool, see the documentation at http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html.

Creating an API Project

Once you have your signing certificate fingerprint, create or modify a project for your application in the Google APIs Console and register for the Maps API.
To get a project and register for the API:
  1. In a browser, navigate to the Google APIs Console.
    • If you haven't used the Google APIs Console before, you're prompted to create a project that you use to track your usage of the Google Maps Android API. ClickCreate Project; the Console creates a new project called API Project. On the next page, this name appears in the upper left hand corner. To rename or otherwise manage the project, click on its name.
    • If you're already using the Google APIs Console, you will immediately see a list of your existing projects and the available services. It's still a good idea to use a new project for Google Maps Android API, so select the project name in the upper left hand corner and then click Create.
  2. You should see a list of APIs and services in the main window. If you don't, select Services from the left navigation bar.
  3. In the list of services displayed in the center of the page, scroll down until you see Google Maps Android API v2. To the right of the entry, click the switch indicator so that it is on.
  4. This displays the Google Maps Android API Terms of Service. If you agree to the terms of service, click the checkbox below the terms of service, then click Accept. This returns you to the list of APIs and services.
You're now ready to get a Maps API key.

Obtaining an API Key

If your application is registered with the Google Maps Android API v2 service, then you can request an API key. It's possible to register more than one key per project.
To get the key:
  1. In the left navigation bar, click API Access.
  2. In the resulting page, click Create New Android Key....
  3. In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application's package name. For example:
    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
  4. The Google APIs Console responds by displaying Key for Android apps (with certificates) followed by a forty-character API key, for example:
    AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
  5. Copy this key value. You will use it in the next step.

Adding the API Key to your application

The final step is to add the API key to your application. It goes in your application's manifest, contained in the file AndroidManifest.xml. From there, the Maps API reads the key value and passes it to the Google Maps server, which then confirms that you have access to Google Maps data.
To add the key to your application:
  1. In AndroidManifest.xml, add the following element as a child of the  element, by inserting it just before the closing tag 
:

    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_api_key"/>
substituting your API key for your_api_key. This element sets the key com.google.android.maps.v2.API_KEY to the value your_api_key and makes the API key visible to any MapFragment in your application.
  • Add the following elements to your manifest. Replace com.example.mapdemo with the package name of your application.
    
            android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
     android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
  • Save AndroidManifest.xml and re-build your application.
  • Specify settings in the Application Manifest

    An Android application that uses the Google Maps Android API needs to specify the following settings in its manifest file, AndroidManifest.xml:
    • Permissions that give the application access to Android system features and to the Google Maps servers.
    • Notification that the application requires OpenGL ES version 2. External services can detect this notification and act accordingly. For example, Google Play Store won't display the application on devices that don't have OpenGL ES version 2.
    • The Maps API key for the application. The key confirms that you've registered with the Google Maps service via the Google APIs Console.
    This section describes each of these settings and how to add them to AndroidManifest.xml.

    Specifying permissions

    Set permissions by adding  elements as children of the  element. The syntax is:
     android:name="permission_name"/>
    
    For example, to request the Internet permission, add:
     android:name="android.permission.INTERNET"/>
    
    Besides permissions required by other parts of your application, you must add the following permissions in order to use the Google Maps Android API:
    The following permissions are recommended, but can be ignored if your application does not access the user's current location, either programmatically, or by enabling the My Location layer.
    • android.permission.ACCESS_COARSE_LOCATION Allows the API to use WiFi or mobile cell data (or both) to determine the device's location.
    • android.permission.ACCESS_FINE_LOCATION Allows the API to use the Global Positioning System (GPS) to determine the device's location to within a very small area.
       android:name="android.permission.INTERNET"/>
       android:name="android.permission.ACCESS_NETWORK_STATE"/>
       android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
      
       android:name="android.permission.ACCESS_COARSE_LOCATION"/>
       android:name="android.permission.ACCESS_FINE_LOCATION"/>
      

    Requiring OpenGL ES version 2

    Because version 2 of the Google Maps Android API requires OpenGL ES version 2, you must add a  element as a child of the  element inAndroidManifest.xml:
    
            android:glEsVersion="0x00020000"
            android:required="true"/>
    
    This notifies external services of the requirement. In particular, it has the effect of preventing Google Play Store from displaying your app on devices that don't support OpenGL ES version 2.

    Add a Map

    After you've added references to the Google Play services SDK, added your key and customized your Android Manifest, you can try adding a map to your application.
    The easiest way to test that your application is configured correctly is to add a simple map. You will have to make changes in two files: main.xml and MainActivity.java. Please note that the code below is only useful for testing your settings in an application targeting Android API 12 or later, This code should not be used in a production application. Examples of how to add more robust code appear throughout this guide and in the sample code.
    1. In main.xml, add the following fragment.
      xml version="1.0" encoding="utf-8"?>
       xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="com.google.android.gms.maps.MapFragment"/>
      
    2. In MainActivity.java, add the following code.
      package com.example.mapdemo;
      import android.app.Activity;
      import android.os.Bundle;
      public class MainActivity extends Activity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
          }
      }
      
    3. Build and run your application. You should see a map. If you don't see a map, confirm that you've completed all of the steps appearing earlier in this document.