如何在进程的主线程上调用onBackPress()

时间:2015-11-02 05:11:18

标签: android multithreading view admob

我是admobi的新手,我正在添加插页式添加,我想以编程方式关闭添加。经过一些研究后我发现它是不可能的,只有这个我们需要使用onbackpress来关闭添加,因为它会在你按下背压键时关闭。我已经尝试了但是它给出了一个像 java.lang.IllegalStateException:必须从进程的主线程调用。 在android.app.Activity.onKeyUp(Activity.java:2131) 我试图解决它从失去的两天它不工作请任何机构解决它,并将它给予我将感谢full.i我在下面添加我的代码

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

3 个答案:

答案 0 :(得分:0)

将你想要从你的活动中进行背压的地方

super.onBackPressed();

答案 1 :(得分:0)

  1. 在主线程的Handler中实现key_up和key_down事件。

  2. 从您的启动线程内部,向已实现的处理程序发送一条消息,以执行key_up和key_down事件。

  3. 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();
            }
        }
    }
    

答案 2 :(得分: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();


                }
            });