2011年8月23日 星期二

AdMob測試

寫完氣壓計測試程式後,接著Jimmy's papa開始測試如何在APP置入AdMob線上廣告,到Google code下載AdMob的SDK及範例程式,參看相關說明文件後,以下是我的程式範例

package com.jimmyscratchlab.androidbarometer;


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class AndroidBarometerActivity extends Activity  {
    /** Called when the activity is first created. */
    private AdView adView;

    private static final String TAG = "sensor";
    private  SensorManager sm;
    private TextView myTextView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create the adView
        adView = new AdView(this, AdSize.BANNER, "你的發佈商ID");

        // Lookup your LinearLayout assuming it’s been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

        // Add the adView to it
        layout.addView(adView);
        
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        request.setTesting(true);//啟用測試模式,也可以在AdMob管理頁面選擇停用測試模式
        adView.loadAd(request);
        
        //-------------------
        myTextView = (TextView) findViewById(R.id.value);
        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        int sensorType = Sensor.TYPE_PRESSURE;
        sm.registerListener(myPressureListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
        
        

    }
    
    @Override
    public void onDestroy() {
      adView.destroy();
      super.onDestroy();
    }

    
    final SensorEventListener myPressureListener = new SensorEventListener(){

        public void onSensorChanged(SensorEvent sensorEvent){
            
            if(sensorEvent.sensor.getType() == Sensor.TYPE_PRESSURE){
                Log.i(TAG,"onSensorChanged");
                
                java.text.DecimalFormat df = new java.text.DecimalFormat("#.#");
                String reading ="PRESSURE: "+ df.format( sensorEvent.values[0] )+"millibars";
                myTextView.setText(reading);
            }
            
        }

        public void onAccuracyChanged(Sensor sensor , int accuracy){
            Log.i(TAG, "onAccuracyChanged");
        }
    };
     
    public void onPause(){
        //此時需關掉sensor,否則會一直運作,直到電力耗盡
        sm.unregisterListener(myAccelerometerListener);
        super.onPause();
    }
    
}

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/value" android:layout_width="fill_parent"
    android:text="@string/hello" android:textSize="14sp"
    android:layout_margin="30dp" android:layout_height="wrap_content"/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jimmyscratchlab.androidbarometer" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidBarometerActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

現在只是測試模式,還不能賺摳摳

寄件者 scratchlab

注意,雖然AdMob有提醒第一次開通需等上兩分鐘,可是Jimmy's papa足足等上一天都盼不到廣告的蹤影,後來只好上AdMob的管理頁面,看是否有哪些選項沒點到,然後點選下圖紅框關於AdSense的選項後,哇!!!好像水庫閘門大開一樣,線上廣告內容源源而至

寄件者 scratchlab

沒有留言:

張貼留言