2015-11-02 27 views
0

我是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(); 
     } 
    } 
} 
+0

可能有人請,因爲沒有人回答 –

回答

0

所以對於這麼多的研究,我發現,通過使用定時器的解決方案的另一種方式,將工作對我來說

Timer timer = new Timer(); 

SwitchPage(6); 

private void SwitchPage(int seconds) { 
     // TODO Auto-generated method stub 
      timer = new Timer(); // At this line a new Thread will be created 
      timer.schedule(new SwitchPageTask(), 10000, seconds * 10000); // delay in milliseconds 
    } 
class SwitchPageTask extends TimerTask { 

     @Override 
     public void run() { 

      // As the TimerTask run on a separate thread from UI thread we have 
      // to call runOnUiThread to do work on UI thread. 
      runOnUiThread(new Runnable() { 
       public void run() { 
        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 
        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); 
        finish(); 
        SwitchPageTask.this.cancel(); 
        Intent intent=new Intent(MainActivity.this,Second.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        SwitchPageTask.this.cancel(); 
        finish(); 


       } 
      }); 
0

calll這個代碼,你想從你的活動

super.onBackPressed(); 
+0

我想你的答案其給予回答這個問題錯誤如java.lang.IllegalStateException:必須從進程的主線程調用。請閱讀我充分的問題,因爲我用代碼來描述,並請給我一個解決方案 –

+0

runOnUiThread(新的Runnable(){@Override 公共 無效的run(){ super.onBackPressed();} }); –

+0

+ Akash kv寫得不好,解釋不好。 – imsrgadich

0
  1. 執行backpress器裏面主線程
  2. 的處理器KEY_UP和key_down的活動從您的飛濺THR內ead向執行的處理程序發送消息以執行key_up和key_down事件。

如果有效,請接受答案。

import android.os.Handler; 

    public class MainActivity extends Activity { 
    private InterstitialAd interstitial; 
    protected boolean active = true; 
    protected int splashtime = 3000; 
    private Handler handler; 

    @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); 
     //Creating new Handler object 
     handler = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));    
     } 
     }; 

     // 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 
       { 
        handler.sendEmptyMessage(0); 
       } 

      } 
     }; 
     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(); 
    } 
    } 
    } 
+0

插頁式加載現在不加載,並且它直接關閉視圖 –

+0

在您遇到崩潰之前是否加載?因爲我沒有改變你的邏輯。我只是用發送消息給handler來代替key_up和key_down。 –

相關問題