我是admobi的新手,我添加插頁式添加,我想以編程方式關閉添加。經過一番研究,我發現這是不可能的,只有這個我們需要使用onbackpress來關閉添加,因爲它會在按下後退鍵時關閉。我試過了,但它給出了一個錯誤,如 java.lang.IllegalStateException :必須從進程的主線程調用。 at android.app.Activity.onKeyUp(Activity.java:2131) 我想解決它從失去的兩天它不工作請任何機構解決它,並給它我會感謝full.i加入我下面如何在進程的主線程上調用onBackPress()
public class MainActivity extends Activity {
private InterstitialAd interstitial;
protected boolean active = true;
protected int splashtime = 3000;
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
//super.onSaveInstanceState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-4412961323059248/9600290618");
final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Adss
/* .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("9F0D0FB0280794109822A582BFFB7EC1")*/
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
Thread splash = new Thread()
{
@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
try
{
int waitid = 0;
while(active && (waitid < splashtime))
{
sleep(1000);
if(active)
{
waitid+=100;
}
}
}
catch (InterruptedException e)
{
// TODO: handle exception
}
finally
{
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}
}
};
splash.start();
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
//interstitial.show();
displayInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
可能有人請,因爲沒有人回答 –