2015-03-27 14 views
5

Tôi có ba hoạt động và tôi đang thực hiện admob cho mỗi hoạt động, mọi hoạt động đều có biểu ngữ riêng và khi hoạt động thay đổi, hoạt động khác bị treo một chút do tải quảng cáo trong nền, là có bất kỳ cách nào mà một biểu ngữ xuất hiện trong tất cả các hoạt động khi chuyển sang để tránh sự chậm trễ.Thực hiện admob cho các hoạt động khác nhau

+0

Các tải quảng cáo trong một thread riêng biệt một cách tự nhiên, ra khỏi hộp. trong tất cả các ứng dụng của tôi, tôi tải quảng cáo một cách cụ thể và sau đó hiển thị quảng cáo khi được tải đầy đủ. –

Trả lời

1

Cách lý tưởng là sử dụng các đoạn cho mỗi screens của bạn. Bằng cách này, bạn sẽ sử dụng một hoạt động duy nhất có một adview duy nhất.

Nếu bạn muốn sử dụng nhiều hoạt động thay vào đó, sau đó thực hiện giải pháp duy nhất tôi biết là sử dụng một phương pháp tĩnh để tải quảng cáo:

public class MyAdView { 
    public static void SetAD(AdView adView){ 
     AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
     adView.loadAd(adRequest); 
    } 

} 

Cách sử dụng:

public class SomeActivity extends Activity { 
    private AdView adView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.caller_main); 
     MyAdView.SetAd((AdView)findViewById(R.id.adView)); 
    } 
} 
+0

Không thể hiểu được –

+0

cách tiếp cận tốt nhất để giữ mã liên quan đến admob dài khỏi hoạt động! cảm ơn bạn, nhưng nó có ý nghĩa để làm cho nó tĩnh? –

14

bạn có thể làm điều đó chỉ cần tải quảng cáo trong lớp ứng dụng và sử dụng nó trong bất kỳ hoạt động nào.

bạn có thể tải demo

như tôi làm điều đó,

App lớp

import android.app.Application; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdView; 

public class App extends Application { 

AdView adView; 

@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 

    super.onCreate(); 

    adView = new AdView(this); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933"); 
    // Request for Ads 
    AdRequest adRequest = new AdRequest.Builder().build(); 

    // Load ads into Banner Ads 
    adView.loadAd(adRequest); 
} 

public void loadAd(LinearLayout layAd) { 

    // Locate the Banner Ad in activity xml 
    if (adView.getParent() != null) { 
     ViewGroup tempVg = (ViewGroup) adView.getParent(); 
     tempVg.removeView(adView); 
    } 

    layAd.addView(adView); 

} 
} 

Hoạt động chính

public class MainActivity extends Activity { 

App app; 
LinearLayout layAd; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    layAd = (LinearLayout) findViewById(R.id.layad); 

    app = (App) getApplication(); 
    app.loadAd(layAd); 

    Button btnNext = (Button) findViewById(R.id.next); 
    btnNext.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent iNext = new Intent(MainActivity.this, 
        SecondActivity.class); 
      startActivity(iNext); 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    app.loadAd(layAd); 
    super.onResume(); 
} 
} 

Hoạt động Thứ hai

public class SecondActivity extends Activity { 

App app; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_second); 

    LinearLayout layAd = (LinearLayout) findViewById(R.id.layad); 

    app = (App) getApplication(); 
    app.loadAd(layAd); 
} 
} 

Manifest xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.admobdemo" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

<application 
    android:name="com.example.admobdemo.App" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.admobdemo.MainActivity" 
     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.example.admobdemo.SecondActivity" 
     android:label="@string/app_name" > 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <activity 
     android:name="com.google.android.gms.ads.AdActivity" 
     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 
</application> 
</manifest> 

bố trí hoạt động chính xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="${relativePackage}.${activityClass}" > 

<LinearLayout 
    android:id="@+id/layad" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
</LinearLayout> 

<Button 
    android:id="@+id/next" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

và hoạt động thứ hai bố trí xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="${relativePackage}.${activityClass}" > 

<LinearLayout 
    android:id="@+id/layad" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
</LinearLayout> 
</LinearLayout> 
+0

nỗ lực để giải thích được đánh giá cao nhưng nó cho thấy lỗi ở chỗ tôi –

+0

Bạn có thể gửi báo lỗi ở đây ...... – RBK

+0

@RaviKoradiya tôi có thể sử dụng bố trí cho AdView đây trong lớp 'Ứng dụng'? – NarendraJi

Các vấn đề liên quan