如何从String资源获取广告单元ID和AddTest设备

时间:2014-09-26 01:33:52

标签: admob

我有一个广告活动,但我想从string.xml获取广告单元ID,任何人都可以告诉我该怎么做?

我在strings.xml文件中有这个字符串资源:

    <string name="KAM">"ca-app-pub-7024462642830025/8524979991"</string>

我想把这个放在这个活动中

package com.JimSoft.Entertainment.Edu.LearnWithEnuma;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

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

public class LetraRActivity extends Activity implements OnInitListener{

/** The view to show the ad. */
private AdView adView;
//Si encuentras este codigo le pertenece a JimSoft Entertainment Ltda.
//ID AdMob
private static final String AD_UNIT_ID = "ca-app-pub-4561187343062246/6585366415";
private TextToSpeech TextToSpeechBienvenida;
private String TextoLetra;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_letra_r);
    TextToSpeechBienvenida = new TextToSpeech( this, this );
    TextoLetra = (String)getString(R.string.ConceptoLetraR);

    // Se crea La Publicidad
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.Publicidad);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("37C65A6A5B1003E8")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
}

private void ParametrosHablar (String str)
{
    TextToSpeechBienvenida.speak( str, TextToSpeech.QUEUE_FLUSH, null );
    TextToSpeechBienvenida.setSpeechRate( -2.0f );
    TextToSpeechBienvenida.setPitch( 0.0f );
}

public void onInit(int status) {
    //Se llama al metodo "cargarPreferencias"
    ParametrosHablar( TextoLetra.toString() );
    if ( status == TextToSpeech.LANG_MISSING_DATA | status == TextToSpeech.LANG_NOT_SUPPORTED )
    {
            Toast.makeText( this, "ERROR LANG_MISSING_DATA | LANG_NOT_SUPPORTED", Toast.LENGTH_SHORT ).show();
    }


}

public void IniciarAtras(View v){
    Intent LanzarLetraQ = new Intent(this, LetraQActivity.class);
    startActivity(LanzarLetraQ);
    overridePendingTransition(R.anim.animacion_derecha_entrada, R.anim.animacion_derecha_salida);
    finish();
}

public void IniciarAdelante(View v){
    Intent LanzarLetraS = new Intent(this, LetraSActivity.class);
    startActivity(LanzarLetraS);
    overridePendingTransition(R.anim.animacion_izquierda_entrada, R.anim.animacion_izquierda_salida);
    finish();
}

@Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Llamada despues de que una actividad es destruida */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();

    if ( TextToSpeechBienvenida != null )
    {
            TextToSpeechBienvenida.stop();
            TextToSpeechBienvenida.shutdown();
    }
    super.onDestroy();
  }

}

1 个答案:

答案 0 :(得分:2)

您可以替换:

adView.setAdUnitId(AD_UNIT_ID);

使用:

adView.setAdUnitId(getResources().getString(R.string.KAM));

您可以对测试设备执行相同的操作。

假设您在下面的字符串中有设备的ID:

 <string name="device_id">37C65A6A5B1003E8</string>

您可以按照以下步骤设置ID:

AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(getResources().getString(R.string.device_id))
        .build();