0

this is my activity, which i would add a Interstitial. I would make that if this activity is onPause for more than 30 seconds, at onResume i want to show the intestial. because through this activity and another, this application must open urls and then as soon you come back to the activity(more than 30 second) I want to add a Interstitial. can be such a thing? If yes, how?

Activity

public class EpisodiActivity extends Activity {

AdView adView;

public class ViewModel {
    private String url;
    private String name;

    public ViewModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return this.name;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.episodi_activity);

 // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    String[] episodi = getIntent().getStringArrayExtra("Product");
    String[] urls = getIntent().getStringArrayExtra("urls");


    ListView mylist = (ListView) findViewById(R.id.listView1);

    // And in this loop we create the ViewModel instances from 
    // the name and url and add them all to a List
    List<ViewModel> models = new ArrayList<ViewModel>();
    for (int i = 0; i < episodi.length; i++) {
        String name = episodi[i];
        String url = "No value";//ok, so bye :Dwait 1 second please you now another code for this
        if (i < urls.length) {
            url = urls[i];
        }
        ViewModel model = new ViewModel(url, name);//, url);
        models.add(model);
    }


    // Here we create the ArrayAdapter and assign it to the ListView
    // We pass the List of ViewModel instances into the ArrayAdapter
    final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);

    mylist.setAdapter(adapter);


    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {

            // Here we get the ViewModel at the given position
            ViewModel model = (ViewModel) arg0.getItemAtPosition(position);

            // And the url from the ViewModel
            String url = model.getUrl();

            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });
}

}

john
  • 97
  • 1
  • 1
  • 9

1 Answers1

1

Save the current time to a shared preference int in onPause. In onResume compare the current time to the saved timed and if more than 30 seconds inflate your interstitial layout.

Could look something like this:

onPause():

super.onPause();

int pauseTime = System.currentTimeMillis();

SharedPreferences sharedPrefs = getSharedPreferences(PREFS, 0);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putInt("pauseTime", pauseTime);
editor.commit();

onResume:

super.onResume();

int currentTime = System.currentTimeMillis();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int pauseTime = sharedPrefs.getInt("pauseTime",0);

int timePassed = currentTime - pauseTime;

if(timePassed >= 30000) {
    // Launch your interstitial activity or call a function in this Activity that brings up your interstitial
}
Randy
  • 955
  • 9
  • 16
  • can you show me with some code's line? add Interstitial I have use a new class? – john Aug 11 '14 at 20:37
  • Do you already have a layout and Activity for your interstitial? – Randy Aug 11 '14 at 20:42
  • yes the activity is what I wrote above, I have to add something in the XML? – john Aug 11 '14 at 20:43
  • If I understand the Activity above correctly, you are displaying a list of episodes and when the user selects an episode you are then triggering an intent to start that episode. Correct? So if the user is in this Activity and they go to onPause and resume >= 30 seconds later, you will then start an intent to bring up an interstitial (most likely an Ad, right?). You already know how to start an intent because you are doing it when someone clicks an episode. Just do that in onResume after the time check. – Randy Aug 11 '14 at 20:49
  • I would like to put an advertisement in the event that a user views an episode for more than 30 seconds, when he return in this activity – john Aug 11 '14 at 20:56
  • Ok, I see. Then just inflate an XML layout that shows your Ad. See my latest edit and just call a function in the if statement that inflates the Ad. – Randy Aug 11 '14 at 20:59