如何在点击菜单后的10秒钟内延迟插入广告

时间:2019-04-25 10:11:41

标签: time load delay

我下面有代码。在我的应用程序中,当我单击菜单中的“关于”按钮时,整页广告就会出现交错。但是google admob向我发送了一封电子邮件,以更改此内容,以免在打开关于页面时在广告上误点击用户。所以我想让广告延迟显示5秒以解决问题。因此,当用户单击“关于菜单”或“菜单”时,广告将在5秒钟后加载。我认为这将解决问题。请查看并为我提供更改代码的方法。 谢谢。这些代码来自mainactivity.java文件。

  

导入android.Manifest;导入android.content.Context;进口   android.content.Intent;导入android.content.pm.PackageManager;   导入android.os.Build;导入android.os.Bundle;进口   android.support.annotation.NonNull;进口   android.support.v4.app.Fragment;进口   android.support.v4.app.FragmentManager;进口   android.support.v4.app.FragmentTransaction;进口   android.support.v4.content.ContextCompat;导入android.view.View;   导入android.support.design.widget.NavigationView;进口   android.support.v4.view.GravityCompat;进口   android.support.v4.widget.DrawerLayout;进口   android.support.v7.app.ActionBarDrawerToggle;进口   android.support.v7.app.AppCompatActivity;进口   android.support.v7.widget.Toolbar;导入android.view.Menu;进口   android.view.MenuItem;进口   android.view.inputmethod.InputMethodManager;进口   android.widget.ListAdapter;导入android.widget.ListView;进口   android.widget.Toast;

     

导入com.google.android.gms.ads.AdRequest;进口   com.google.android.gms.ads.AdView;进口   com.google.android.gms.ads.InterstitialAd;进口   com.osmanoslanoglu.ofarsi.fragment.FavoriFragment;进口   com.osmanoslanoglu.ofarsi.fragment.HakkındaFragment;进口   com.osmanoslanoglu.ofarsi.fragment.HistorySearcWordListFragment;   导入com.osmanoslanoglu.ofarsi.fragment.IletisimFragment;进口   com.osmanoslanoglu.ofarsi.fragment.SearchFragment;进口   com.osmanoslanoglu.ofarsi.fragment.SendNewWord;进口   com.osmanoslanoglu.ofarsi.model.Word;

     

import java.util.List;

     

公共类MainActivity扩展了AppCompatActivity实现   NavigationView.OnNavigationItemSelectedListener {

private AdView adView;
private AdRequest adRequest;

private InterstitialAd gecisReklam;
Fragment fragment;
ListView listView;
ListAdapter listAdapter;
List<Word>wordList;
final static int PERMISSIONS_REQUEST_CODE = 1;

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


}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getPermissionWriteExternalStorage();
    getPermissionReadExternalStorage();

    //Burda AdView objesini oluşturuyoruz ve anasayfa.xml de oluşturduğumuz adView e bağlıyoruz
    gecisReklam = new InterstitialAd(this);
    gecisReklam.setAdUnitId("ca-app-pub-1022160813397117/1942240160");//Reklam
     

miz.Admob daoluşturduğumuzgeçişreklam id si           loadGecisReklam();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.app_name,
     

R.string.app_name){

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
     

0);               }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
     

0);               }           };

    toolbar.setTitleMarginEnd(10);
    toolbar.setLogo(R.drawable.ferheng_logo_y_png_small_ks);
    drawer.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

    if (fragment ==null){
        fragment = new SearchFragment();
        if (getIntent().getExtras() != null){
            fragment.setArguments(getIntent().getExtras());
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentPalace, fragment);
        fragmentTransaction.commit();
        //getSupportActionBar().setTitle("");

        // set the toolbar title
    }



    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}



      @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
     int id = item.getItemId();

    if (id == R.id.action_settings) {

        Intent i = new Intent(getApplicationContext(),Setting.class);
        startActivity(i);

        return true;
    }

    return super.onOptionsItemSelected(item);
}




@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    displaySelectedScreen(item.getItemId());

    return true;
}

private void displaySelectedScreen(int itemId) {

    Fragment fragment = null;
    String title ="";

    switch (itemId) {
        case R.id.wordSearch:

            gecisReklam.show();
            fragment = new SearchFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentPalace, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();



            title = getString(R.string.sKelimeAra);
            break;
        case R.id.wordHistory:

            gecisReklam.show();
            fragment = new HistorySearcWordListFragment();
            title = getString(R.string.sHistory);
            break;
        case R.id.WordNew:

            gecisReklam.show();
            fragment = new SendNewWord();
            title = getString(R.string.sYeniKelimeGonder);
            break;
        case R.id.wordFavoriList:
            gecisReklam.show();
            fragment = new FavoriFragment();//favori
            title = getString(R.string.sFavori);
            break;
        case R.id.nav_share:


            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = getResources().getString(R.string.appPath);
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
     

“Uygulamalalarımızıindirip puan vererek destek olabilirsiniz。”);                   SharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody);                   字符串appname = getResources()。getString(R.string.app_name);                   startActivity(Intent.createChooser(sharingIntent,appname +“UygulamamızıPaylaşın。”));

           // fragment = new HakkındaFragment();
            title = getString(R.string.sPaylas);
            break;
        case R.id.info:
            fragment = new HakkındaFragment();
            title = getString(R.string.sHakkinda);
            break;
        case R.id.iletisim:
            fragment = new IletisimFragment();
            title = getString(R.string.sIletisim);
            break;
    }


    if (fragment != null) {
        loadGecisReklam();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentPalace, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}


public void loadGecisReklam() {

    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("96C2D5822E31C460C6FE0809EDB63C50")
            .build();

    //Reklam Yükleniyor
    gecisReklam.loadAd(adRequest);
}

/** reklamGoster butonu tıklanınca çalışacak. */
public void showGecisReklam() {
    // Tekrar reklam yüklenene kadar disable edilecek

    if (gecisReklam.isLoaded()) {//Eğer reklam yüklenmişse kontrol ediliyor
        gecisReklam.show(); //Reklam yüklenmişsse gösterilecek
    } else {//reklam yüklenmemişse
    }
}



public void getPermissionWriteExternalStorage() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    PERMISSIONS_REQUEST_CODE);
        }
    }
}

public void getPermissionReadExternalStorage() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    PERMISSIONS_REQUEST_CODE);

        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        } else {
            Toast.makeText(this, "izin verilmedi.!!!", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
     

}

0 个答案:

没有答案