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

问题描述 投票:0回答:3

我是 admobi 新手,我正在添加插页式添加,我想以编程方式关闭添加。经过一番研究,我发现这是不可能的,只有这个我们需要使用 onbackpress 来关闭添加,因为当您按下 backpress 键时它会关闭。我已经尝试过,但它给出了类似的错误 java.lang.IllegalStateException:必须从进程的主线程调用。 在 android.app.Activity.onKeyUp(Activity.java:2131) 我正在尝试从失去的两天中解决它,但它不起作用,请任何人解决它并将其交给我,我将非常感谢。我在下面添加我的代码

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();
        }
    }
}
android multithreading view admob
3个回答
1
投票

因此,经过这么多研究,我找到了另一种解决方案,即使用计时器,这对我有用

调整这个答案

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
投票
  1. 在主线程的Handler内部实现key_up和key_down事件。

  2. 从启动线程内部,向已实现的处理程序发送一条消息以执行 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();
        }
    }
}

-1
投票

在您想要从活动中执行后压的地方调用此方法

super.onBackPressed();
© www.soinside.com 2019 - 2024. All rights reserved.