强大的文字根据Google的指南,我们不建议在应用加载或退出时展示广告。
如何在应用点击按钮设置上显示admob插页式广告或仅播放一次?
ResultLoadListner, OnItemClickListener,OnTaskCompleted,OnClickListener {
private ListView listView;
private ListAdapter adapter;
public static List<RingtoneDetails> ringtons;
private InterstitialAd interstitial;
private ImageView stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialise the varialbes
this.initialiseVariables();
setContentView(R.layout.layout_home_activity);
listView = (ListView) this.findViewById(R.id.main_grid_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
stop = (ImageView) findViewById(R.id.stop);
stop.setOnClickListener(this);
/***/
/*-----------------admob code------------*/
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
interstitial = new InterstitialAd(getBaseContext());
interstitial.setAdUnitId(getResources().getString(
R.string.interstitial_id));
interstitial.loadAd(request);
// startloading results
LoadRingtonThread thread = new LoadRingtonThread(HomeActivity.this);
thread.execute("");
}
public void initialiseVariables() {
ringtons = new ArrayList<RingtoneDetails>();
adapter = new ListAdapter(HomeActivity.this, ringtons);
// SOURCE_URL = this.getResources().getString(R.string.source_url);
}
@Override
public void onResultLoad(RingtoneDetails... details) {
for(RingtoneDetails detail:details){
ringtons.add(detail);
this.adapter.notifyDataSetChanged();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
RingtonePlayer player=new RingtonePlayer(ringtons.get(position).getRingtoneid(),HomeActivity.this);
player.startPlaying();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (SettingDialog.CONTACT_CHOOSER_ACTIVITY_CODE):
if (resultCode == Activity.RESULT_OK) {
try {
Uri contactData = data.getData();
String contactId = contactData.getLastPathSegment();
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER, };
Cursor localCursor = getContentResolver().query(
contactData, PROJECTION, null, null, null);
localCursor.moveToFirst();
String contactID = localCursor.getString(localCursor
.getColumnIndexOrThrow("_id"));
String contactDisplayName = localCursor.getString(localCursor
.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,
contactID);
localCursor.close();
ContentValues localContentValues = new ContentValues();
localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE,
SettingDialog.filePath);
getContentResolver().update(localUri, localContentValues,
null, null);
Toast.makeText(this,
"Ringtone assigned to: " + contactDisplayName,
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
break;
}
}
@Override
public void onBackPressed() {
if (interstitial.isLoaded()) {
interstitial.show();
}
super.onBackPressed();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RingtonePlayer.stopPlaying();
stop.setVisibility(ImageView.GONE);
}
@Override
public void onTaskCompleted() {
// TODO Auto-generated method stub
stop.setVisibility(ImageView.GONE);
}
@Override
public void onTaskStart() {
stop.setVisibility(ImageView.VISIBLE);
}
}
答案 0 :(得分:0)
您从onBackPressed
删除此代码并将其添加到onClick
处理程序。
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final Button myButton = ..
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
}