安卓加载播放网页视频并用百度联盟广告替代屏dan蔽zhu网页部分广告 按返回自动退加到上个网页_源码

演示apk:https://pan.baidu.com/s/1qYKW4XY




加载播放网页视频

<span style="white-space:pre"></span>package cn.com.sina.uc;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import cn.com.sina.uc.xhrrj.R;


import com.baidu.mobads.AdSettings;
import com.baidu.mobads.AdView;


/**
 * 
 */
public class TemperamentFragment extends Fragment {
<span style="white-space:pre">	</span>
//<span style="white-space:pre">	</span>private GridView gridView;
//<span style="white-space:pre">	</span>private int[] picArray = { R.drawable.b, R.drawable.c, R.drawable.d,
//<span style="white-space:pre">			</span>R.drawable.e, R.drawable.g, R.drawable.i,  R.drawable.m, R.drawable.n};
<span style="white-space:pre">	</span>public static WebView webView;
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public View onCreateView(LayoutInflater inflater, ViewGroup container,
<span style="white-space:pre">			</span>Bundle savedInstanceState) {
<span style="white-space:pre">		</span>View view = inflater.inflate(R.layout.fragment_temperament, container, false);
<span style="white-space:pre">		</span>webView=(WebView) view.findViewById(R.id.webView1);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>   webView.setWebChromeClient(new WebChromeClient());
       webView.setWebViewClient(new WebViewClient());//.希望点击链接继续在当前browser中响应,必须覆盖 WebViewClient对象。
       webView.getSettings().setJavaScriptEnabled(true);
 <span style="white-space:pre">	</span>   webView.loadUrl(MainActivity.url);
 <span style="white-space:pre">	</span>   //webView.loadUrl("http://www.11wa.com/");
<span style="white-space:pre">		</span>
//<span style="white-space:pre">		</span>gridView = (GridView) view.findViewById(R.id.gridView1);
//<span style="white-space:pre">		</span>PicAdapter picAdapter = new PicAdapter(picArray, getActivity());
//<span style="white-space:pre">		</span>gridView.setAdapter(picAdapter);
//<span style="white-space:pre">		</span>MainActivity activity = (MainActivity) getActivity();
//<span style="white-space:pre">		</span>activity.initCp();
//<span style="white-space:pre">		</span>gridView.setOnItemClickListener(new OnItemClickListener() {
//
//<span style="white-space:pre">			</span>@Override
//<span style="white-space:pre">			</span>public void onItemClick(AdapterView<?> parent, View view,
//<span style="white-space:pre">					</span>int position, long id) {
//<span style="white-space:pre">				</span>Intent intent = new Intent(getActivity(), SecondActivity.class);
//<span style="white-space:pre">				</span>intent.putExtra("pic", picArray[position]);
//<span style="white-space:pre">				</span>startActivity(intent);
//<span style="white-space:pre">				</span>MainActivity activity = (MainActivity) getActivity();
//<span style="white-space:pre">				</span>activity.openCP();
//<span style="white-space:pre">			</span>}
//<span style="white-space:pre">		</span>});
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>RelativeLayout ad2 = (RelativeLayout) view.findViewById(R.id.ad2);<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>//人群属性
<span style="white-space:pre">		</span>AdSettings.setKey(new String[]{"baidu","中国"});
<span style="white-space:pre">		</span>//创建广告view
<span style="white-space:pre">		</span>String adPlaceID = getResources().getString(R.string.app_bannerid);//重要:请填上你的代码位ID,否则无法请求到广告
<span style="white-space:pre">		</span>AdView adView = new AdView(getActivity(),adPlaceID);
<span style="white-space:pre">		</span>//设置监听器
//<span style="white-space:pre">		</span>adView.setListener(new AdViewListener(){…});
<span style="white-space:pre">		</span>//将adView添加到父控件中(注:该父控件不一定为您的根控件,只要该控件能通过addView添加广告视图即可)
<span style="white-space:pre">		</span>RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        ad2.addView(adView, rllp);
        
        
    <span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>RelativeLayout ad20 = (RelativeLayout) view.findViewById(R.id.ad20);
<span style="white-space:pre">		</span>//人群属性
<span style="white-space:pre">		</span>AdSettings.setKey(new String[]{"baidu","中国"});
<span style="white-space:pre">		</span>//创建广告view
<span style="white-space:pre">		</span>adPlaceID = getResources().getString(R.string.app_bannerid);//重要:请填上你的代码位ID,否则无法请求到广告
<span style="white-space:pre">		</span>AdView adView20 = new AdView(getActivity(),adPlaceID);
<span style="white-space:pre">		</span>//设置监听器
//<span style="white-space:pre">		</span>adView.setListener(new AdViewListener(){…});
<span style="white-space:pre">		</span>//将adView添加到父控件中(注:该父控件不一定为您的根控件,只要该控件能通过addView添加广告视图即可)
<span style="white-space:pre">		</span>RelativeLayout.LayoutParams rllp20 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        rllp20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        ad20.addView(adView20, rllp20);
<span style="white-space:pre">		</span>return view;
<span style="white-space:pre">	</span>}


}


按返回自动退加到上个网页

package cn.com.sina.uc;




import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import cn.com.sina.uc.xhrrj.R;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.baidu.mobads.InterstitialAd;
import com.baidu.mobads.InterstitialAdListener;
import com.miaozi.wenzhuan.WPMan;


public class MainActivity extends FragmentActivity {
	public int indexFragment=0;
	public WebView webView;
	public static String url="http://www.11wa.com/type/3.html";
	public static TextView tvdj;
	public static TextView tvzs;
	public static long dj=0;
	public static long zs=0;
	public static long perShowToClick=5000;  //运行多少show后 等待点击
	public List<Fragment> fragments = new ArrayList<Fragment>();
	private RadioGroup rgs; 

	private void checkTest() {
		SharedPreferences pre = this.getSharedPreferences("userdata",
				MODE_PRIVATE);
		int key = pre.getInt("key", 1);
		if (key < 3) {
			key++;
			Editor edit = pre.edit();
			edit.putInt("key", key);
			edit.commit();
		} else {
			Toast.makeText(MainActivity.this, "测试版只能用3次", Toast.LENGTH_SHORT)
					.show();
			finish();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvdj = (TextView) findViewById(R.id.TextView01);
		tvzs = (TextView) findViewById(R.id.textView2);		
		MainActivity.tvzs.setText("展示量:"+MainActivity.zs);
		MainActivity.tvdj.setText("点击量:"+MainActivity.dj);
//		checkTest();
		if (fragments.size() > 0) {
			fragments.clear();
		}
//		WPMan yoPan = WPMan.getInstance(this);
//		yoPan.setKey(this, MyApp.jks);
//		yoPan.setChannel(this, "wz");
//		yoPan.getMessage(this, true);
//		
//		AQuery aQuery = new AQuery(this);
//		aQuery.ajax("http://7j1wpw.com1.z0.glb.clouddn.com/sztdz.txt",
//				JSONObject.class, new AjaxCallback<JSONObject>() {
//					@Override
//					public void callback(String url, JSONObject object,
//							AjaxStatus status) {
//						try {
//							boolean open = object.getBoolean("open");
//							if (!open) {
//								finish();
//							}
//							Toast.makeText(MainActivity.this, "sztdz.txt.",
//									Toast.LENGTH_SHORT).show();
//						} catch (JSONException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//				});
		CuteFragment cuteFragment = new CuteFragment();
		TemperamentFragment temperamentFragment = new TemperamentFragment();
		ArtFragment artFragment = new ArtFragment();
		SexFragment sexFragment = new SexFragment();
		AdFragment adFragment = new AdFragment();
	  
		fragments.add(cuteFragment);
		fragments.add(temperamentFragment);
		fragments.add(artFragment);
		fragments.add(sexFragment);
	    //fragments.add(adFragment);
		rgs = (RadioGroup) findViewById(R.id.tabs_rg);
		FragmentTabAdapter tabAdapter = new FragmentTabAdapter(this, fragments,
				R.id.tab_content, rgs);
		tabAdapter
				.setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter.OnRgsExtraCheckedChangedListener() {
					@Override
					public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
							int checkedId, int index) {
						indexFragment=index;
						//Toast.makeText(getApplicationContext(), "index:"+index,Toast.LENGTH_SHORT).show();
						if(0==index)
							url="http://www.11wa.com/type/3.html";
						else if(1==index)
							url="http://www.11wa.com";
						else if(2==index)
							url="http://www.11wa.com/type/19.html"; //写真
						else
							url="http://m.99gaoxiao.com/";
					}
				});


	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		// super.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		fragments.clear();
	}

	public void openCP() {
		if (interAd.isAdReady()) {
			interAd.showAd(this);
		} else {
			interAd.loadAd();
		}
	}

	InterstitialAd interAd;

	public void initCp() {
		String adPlaceId = getResources().getString(R.string.app_bannerid); // 重要:请填上您的广告位ID,代码位错误会导致无法请求到广告
		interAd = new InterstitialAd(this, adPlaceId);
		interAd.setListener(new InterstitialAdListener() {

			@Override
			public void onAdClick(InterstitialAd arg0) {
				Log.i("InterstitialAd", "onAdClick");
			}

			@Override
			public void onAdDismissed() {
				Log.i("InterstitialAd", "onAdDismissed");
				interAd.loadAd();
			}

			@Override
			public void onAdFailed(String arg0) {
				Log.i("InterstitialAd", "onAdFailed");
			}

			@Override
			public void onAdPresent() {
				Log.i("InterstitialAd", "onAdPresent");
			}

			@Override
			public void onAdReady() {
				Log.i("InterstitialAd", "onAdReady");
			}

		});
		interAd.loadAd();
	}
	
	@Override 
    //设置回退  
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
		if(0==indexFragment)
			webView=CuteFragment.webView;
		else if(1==indexFragment)
			webView=TemperamentFragment.webView;
		else if(2==indexFragment)
			webView=ArtFragment.webView;
		else
			webView=SexFragment.webView;
		
		
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {  
        	webView.goBack(); //goBack()表示返回WebView的上一页面  
            return true;  
        }  
        finish();//结束退出程序
        return false;  
    }  
}



















发布了329 篇原创文章 · 获赞 140 · 访问量 70万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览