按下按钮后会立即显示非页内广告,想要添加延迟

时间:2017-11-30 12:42:43

标签: android popup google-play-services delay interstitial

我在应用中按下播放按钮后设置了插页式广告。 好的,这样运行完美,但按下按钮后会立即显示插页式广告。

有没有办法在弹出之前添加10秒的延迟? IE浏览器。用户按播放,并在插页式广告出现后10秒。

在代码中我有以下几行:

public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
    if (interstitial.isLoaded()) {

    }
}
public void onClickPlayButton(View view) {
    radioService.play();
    interstitial.show();
}

这是我的完整MainActity.java

public class MainActivity extends BaseActivity {

private InterstitialAd interstitial;    
private static boolean displayAd;
private Button playButton;
private Button pauseButton;
private Button stopButton;
private Button nextButton;
private Button previousButton;
private ImageView stationImageView;

private TextView titleTextView;
private TextView albumTextView;
private TextView artistTextView;
private TextView trackTextView;
private TextView statusTextView;
private TextView timeTextView;

private Intent bindIntent;
private TelephonyManager telephonyManager;
private boolean wasPlayingBeforePhoneCall = false;
private RadioUpdateReceiver radioUpdateReceiver;
public static RadioService radioService;
private AdView mAdView;

private String STATUS_BUFFERING;
private static final String TYPE_AAC = "aac";
private static final String TYPE_MP3 = "mp3";
private Handler handler;
private AudioManager leftAm;
private SeekBar volControl;
private ContentObserver mVolumeObserver;

public static int stationID = 0;
public static boolean isStationChanged = false;

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

    interstitial = new InterstitialAd(MainActivity.this);
    interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXXX");


AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();

    mAdView.loadAd(adRequest);

    interstitial.loadAd(adRequest);

    new Handler().postDelayed(new Runnable(){

        @Override
        public void run() {
        displayInterstitial();
        }
    }, 60000);

    // Bind to the service
    try {
        bindIntent = new Intent(this, RadioService.class);
        bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception e) {

    }

    telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (telephonyManager != null) {
        telephonyManager.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    handler = new Handler();
    initialize();

    // Prepare an Interstitial Ad Listener
    //interstitial.setAdListener(new AdListener() {
        //public void onAdLoaded() {
            // Call displayInterstitial() function
            //displayInterstitial();
        //}
    //});
}
///public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
    ///if (interstitial.isLoaded()) {
        ///interstitial.show();
    ///}
///}

// agrega boton share a la app
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
        MenuItem shareItem = (MenuItem) menu.findItem(R.id.action_share);

        ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Listen to my radio https://play.google.com/store/apps/details?id=radio");

        mShare.setShareIntent(shareIntent);
    }
    return true;
}

// fin boton share
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT
            || newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        try {
            setContentView(R.layout.activity_main);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    initialize();

                    if (radioService.getTotalStationNumber() <= 1) {
                        nextButton.setEnabled(false);
                        nextButton.setVisibility(View.INVISIBLE);
                        previousButton.setEnabled(false);
                        previousButton.setVisibility(View.INVISIBLE);
                    }

                    updateStatus();
                    updateMetadata();
                    updateAlbum();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void initialize() {
    try {

        displayAd = Boolean.parseBoolean(getResources().getString(
                R.string.is_display_ad));

        STATUS_BUFFERING = getResources().getString(
                R.string.status_buffering);

        playButton = (Button) this.findViewById(R.id.PlayButton);
        pauseButton = (Button) this.findViewById(R.id.PauseButton);
        stopButton = (Button) this.findViewById(R.id.StopButton);
        nextButton = (Button) this.findViewById(R.id.NextButton);
        previousButton = (Button) this.findViewById(R.id.PreviousButton);
        pauseButton.setEnabled(false);
        pauseButton.setVisibility(View.INVISIBLE);
        stationImageView = (ImageView) findViewById(R.id.stationImageView);

        playButton.setEnabled(true);
        stopButton.setEnabled(false);

        titleTextView = (TextView) this.findViewById(R.id.titleTextView);
        albumTextView = (TextView) this.findViewById(R.id.albumTextView);
        artistTextView = (TextView) this.findViewById(R.id.artistTextView);
        trackTextView = (TextView) this.findViewById(R.id.trackTextView);
        statusTextView = (TextView) this.findViewById(R.id.statusTextView);
        timeTextView = (TextView) this.findViewById(R.id.timeTextView);
        // volume
        leftAm = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        int maxVolume = leftAm
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = leftAm.getStreamVolume(AudioManager.STREAM_MUSIC);
        volControl = (SeekBar) findViewById(R.id.volumebar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onProgressChanged(SeekBar a0, int a1,
                    boolean a2) {
                leftAm.setStreamVolume(AudioManager.STREAM_MUSIC,
                        a1, 0);
            }
        });

        Handler mHandler = new Handler();
        // in onCreate put
        mVolumeObserver = new ContentObserver(mHandler) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                if (volControl != null && leftAm != null) {
                    int volume = leftAm
                            .getStreamVolume(AudioManager.STREAM_MUSIC);
                    volControl.setProgress(volume);
                }
            }

        };
        this.getContentResolver()
        .registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS[AudioManager.STREAM_MUSIC]),
                false, mVolumeObserver);

        // vloume
        startService(new Intent(this, RadioService.class));

        displayAd();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    volControl = (SeekBar) findViewById(R.id.volumebar);
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        int index = volControl.getProgress();
        volControl.setProgress(index + 1);
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        int index = volControl.getProgress();
        volControl.setProgress(index - 1);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public void displayAd() {
    if (displayAd == true) {
        // Create the adView
        try {

            if (mAdView != null) {
                mAdView.destroy();
            }

            mAdView = (AdView) findViewById(R.id.adView);
            mAdView.loadAd(new AdRequest.Builder().build());


        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        LinearLayout layout = (LinearLayout) findViewById(R.id.adLayout);
        layout.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
        layout.setVisibility(View.INVISIBLE);
    }
}

public void updatePlayTimer() {
    timeTextView.setText(radioService.getPlayingTime());

    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    timeTextView.setText(radioService.getPlayingTime());
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 1000);
}
public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
    if (interstitial.isLoaded()) {

    }
}
public void onClickPlayButton(View view) {
    radioService.play();
    interstitial.show();
}

public void onClickPauseButton(View view) {
    radioService.pause();
}

public void onClickStopButton(View view) {
    radioService.stop();
    resetMetadata();
    updateDefaultCoverImage();
}

public void onClickNextButton(View view) {
    resetMetadata();
    playNextStation();
    updateDefaultCoverImage();
}

public void onClickPreviousButton(View view) {
    resetMetadata();
    playPreviousStation();
    updateDefaultCoverImage();
}

public void playNextStation() {
    radioService.stop();
    radioService.setNextStation();
    /*
     * if(radioService.isPlaying()) {
     * radioService.setStatus(STATUS_BUFFERING); updateStatus();
     * radioService.stop(); radioService.play(); } else {
     * radioService.stop(); }
     */
}

public void playPreviousStation() {
    radioService.stop();
    radioService.setPreviousStation();
    /*
     * if(radioService.isPlaying()) {
     * radioService.setStatus(STATUS_BUFFERING); updateStatus();
     * radioService.stop(); radioService.play(); } else {
     * radioService.stop(); }
     */
}

public void updateDefaultCoverImage() {
    try {
        String mDrawableName = "station_"
                + (radioService.getCurrentStationID() + 1);
        int resID = getResources().getIdentifier(mDrawableName, "drawable",
                getPackageName());

        int resID_default = getResources().getIdentifier("station_default",
                "drawable", getPackageName());

        if (resID > 0)
            stationImageView.setImageResource(resID);
        else
            stationImageView.setImageResource(resID_default);

        albumTextView.setText("");
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void updateAlbum() {
    String album = radioService.getAlbum();
    String artist = radioService.getArtist();
    String track = radioService.getTrack();
    Bitmap albumCover = radioService.getAlbumCover();

    albumTextView.setText(album);

    if (albumCover == null || (artist.equals("") && track.equals("")))
        updateDefaultCoverImage();
    else {
        stationImageView.setImageBitmap(albumCover);
        radioService.setAlbum(LastFMCover.album);

        if (radioService.getAlbum().length()
                + radioService.getArtist().length() > 50) {
            albumTextView.setText("");
        }
    }
}

public void updateMetadata() {
    String artist = radioService.getArtist();
    String track = radioService.getTrack();
    // if(artist.length()>30)
    // artist = artist.substring(0, 30)+"...";
    artistTextView.setText(artist);
    trackTextView.setText(track);
    albumTextView.setText("");
}

public void resetMetadata() {
    radioService.resetMetadata();
    artistTextView.setText("");
    albumTextView.setText("");
    trackTextView.setText("");
}

@Override
protected void onDestroy() {

    super.onDestroy();

    if (radioService != null) {
        if (!radioService.isPlaying() && !radioService.isPreparingStarted()) {
            // radioService.stopSelf();
            radioService.stopService(bindIntent);
        }
    }

    if (mAdView != null) {
        mAdView.destroy();
    }

    if (telephonyManager != null) {
        telephonyManager.listen(phoneStateListener,
                PhoneStateListener.LISTEN_NONE);
    }

    unbindDrawables(findViewById(R.id.RootView));
    Runtime.getRuntime().gc();
}

private void unbindDrawables(View view) {
    try {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (radioUpdateReceiver != null)
        unregisterReceiver(radioUpdateReceiver);
    // finish();
}

@Override
protected void onResume() {
    super.onResume();

    /* Register for receiving broadcast messages */
    if (radioUpdateReceiver == null)
        radioUpdateReceiver = new RadioUpdateReceiver();
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_CREATED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_DESTROYED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_STARTED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_CONNECTING));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_START_PREPARING));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_PREPARED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_PLAYING));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_PAUSED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_STOPPED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_COMPLETED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_ERROR));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_BUFFERING_START));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_BUFFERING_END));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_METADATA_UPDATED));
    registerReceiver(radioUpdateReceiver, new IntentFilter(
            RadioService.MODE_ALBUM_UPDATED));

    if (wasPlayingBeforePhoneCall) {
        radioService.play();
        wasPlayingBeforePhoneCall = false;
    }

    if (radioService != null) {
        if (isStationChanged) {
            if (stationID != radioService.getCurrentStationID()) {
                radioService.stop();
                radioService.setCurrentStationID(stationID);
                resetMetadata();
                updateDefaultCoverImage();
            }
            if (!radioService.isPlaying())
                radioService.play();
            isStationChanged = false;
        }
    }
}

/* Receive Broadcast Messages from RadioService */
private class RadioUpdateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(RadioService.MODE_CREATED)) {

        } else if (intent.getAction().equals(RadioService.MODE_DESTROYED)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            updateDefaultCoverImage();
            updateMetadata();
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_STARTED)) {
            pauseButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);

            playButton.setEnabled(true);
            stopButton.setEnabled(false);
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_CONNECTING)) {
            pauseButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            playButton.setEnabled(false);
            stopButton.setEnabled(true);
            updateStatus();
        } else if (intent.getAction().equals(
                RadioService.MODE_START_PREPARING)) {
            pauseButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            playButton.setEnabled(false);
            stopButton.setEnabled(true);
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_PREPARED)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            updateStatus();
        } else if (intent.getAction().equals(
                RadioService.MODE_BUFFERING_START)) {
            updateStatus();
        } else if (intent.getAction().equals(
                RadioService.MODE_BUFFERING_END)) {
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_PLAYING)) {
            if (radioService.getCurrentStationType().equals(TYPE_AAC)) {
                playButton.setEnabled(false);
                stopButton.setEnabled(true);
            } else {
                playButton.setEnabled(false);
                pauseButton.setEnabled(true);
                stopButton.setEnabled(true);
                playButton.setVisibility(View.INVISIBLE);
                pauseButton.setVisibility(View.VISIBLE);
            }
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_PAUSED)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(true);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_STOPPED)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_COMPLETED)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            // radioService.setCurrentStationURL2nextSlot();
            // radioService.play();
            updateStatus();
        } else if (intent.getAction().equals(RadioService.MODE_ERROR)) {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            stopButton.setEnabled(false);
            playButton.setVisibility(View.VISIBLE);
            pauseButton.setVisibility(View.INVISIBLE);
            updateStatus();
        } else if (intent.getAction().equals(
                RadioService.MODE_METADATA_UPDATED)) {
            updateMetadata();
            updateStatus();
            updateDefaultCoverImage();
        } else if (intent.getAction().equals(
                RadioService.MODE_ALBUM_UPDATED)) {
            updateAlbum();
        }
    }
}

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            wasPlayingBeforePhoneCall = radioService.isPlaying();
            radioService.stop();
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            if (wasPlayingBeforePhoneCall) {
                radioService.play();
            }
        } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            // A call is dialing,
            // active or on hold
            wasPlayingBeforePhoneCall = radioService.isPlaying();
            radioService.stop();
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};

public void updateStatus() {
    String status = radioService.getStatus();
    if (radioService.getTotalStationNumber() > 1) {
        /*
         * if (status != "") status = radioService.getCurrentStationName() +
         * " - " + status; else status =
         * radioService.getCurrentStationName();
         */
        String title = radioService.getCurrentStationName();
        try {
            titleTextView.setText(title);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    try {
        statusTextView.setText(status);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// Handles the connection between the service and activity
private final ServiceConnection radioConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        radioService = ((RadioService.RadioBinder) service).getService();

        if (radioService.getTotalStationNumber() <= 1) {
            nextButton.setEnabled(false);
            nextButton.setVisibility(View.INVISIBLE);
            previousButton.setEnabled(false);
            previousButton.setVisibility(View.INVISIBLE);
        }

        updateStatus();
        updateMetadata();
        updateAlbum();
        updatePlayTimer();
        radioService.showNotification();

        /*
         * Uncomment following line if you want auto-play while starting the
         * app
         */
        // radioService.play();
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        radioService = null;
    }
};

}

3 个答案:

答案 0 :(得分:0)

使用Handler。看到这段代码:

new Handler().postDelayed(new Runnable(){
    public void run() {
        //Your your code here.
    }
}, 5000); //Here 5000 is the time delay. Change it accordingly.

答案 1 :(得分:0)

Kvaibhav01的答案将有效。您也可以使用Timer:

mTimer = new Timer();
mTimer.schedule(new TimerTask() {
    public void run() {
        //your code here
    }
}, 1000); //delay in ms

答案 2 :(得分:0)

我尝试了处理程序,现在正常工作(我搞砸了订单)

这是正确的代码:

    public void onClickPlayButton(View view) {
    radioService.play();
    new Handler().postDelayed(new Runnable(){
        public void run() {
        interstitial.show();
        }
    }, 10000);

非常感谢您的帮助!