Android TestingMode 2.0

出自TAMedia
跳至導覽 跳至搜尋
  • 若需要進行廣告投放的測試,可以測試模式來請求廣告, ※測試模式將不會紀錄任何的曝光以及點擊數據
 // 請在將要載入廣告所使用的TWMAdView / TWMInterstitialAd / TWMVideoAd 以下列的方式進行廣告請求, 下面以TWMAdView (Banner Ad)來進行示範
 
 TWMAdView adView = new TWMAdView(this, {YOUR_ADUNIT_ID_BANNER});
 
 TWMAdRequest request = new TWMAdRequest();
 deviceId = getDeviceId(getBaseContext());
 request.addTestDevice(deviceId); // 加入測試的手機Id
 adView.loadAd(request);
  • 取得手機Id
 private String getDeviceId(final Context context) {
      String deviceId = null;
      try {
              deviceId = android.provider.Settings.Secure.getString(
                              context.getContentResolver(), Secure.ANDROID_ID);
              if (deviceId!= null) {
                 return convertToMD5ID(deviceId);
              }
      } catch (Exception e) {
              Log.e("getDeviceId", e.getMessage(), e);
      }
      return deviceId;
  }
  private String convertToMD5ID(final String string) {
      if (string == null) {
              return null;
      }
      try {
          MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(string.getBytes());
            BigInteger number = new BigInteger(1, md.digest());
            String md5 = number.toString(16);
            while (md5.length() < 32)
          md5 = "0" + md5;
          return md5;
      } catch (NoSuchAlgorithmException e) {
          return null;
      }
  }