<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>全域變數 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E5%85%A8%E5%9F%9F%E8%AE%8A%E6%95%B8/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 02 Jul 2019 05:44:08 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>全域變數 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>繼承Application達到Global Variable</title>
		<link>https://noter.tw/1911/%e7%b9%bc%e6%89%bfapplication%e9%81%94%e5%88%b0global-variable/</link>
					<comments>https://noter.tw/1911/%e7%b9%bc%e6%89%bfapplication%e9%81%94%e5%88%b0global-variable/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 01 Feb 2012 12:58:36 +0000</pubDate>
				<category><![CDATA[手機 App]]></category>
		<category><![CDATA[Application]]></category>
		<category><![CDATA[global variable]]></category>
		<category><![CDATA[全域變數]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[ads]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=1911</guid>

					<description><![CDATA[<p>(2012-02-04 更新)Android軟體要達到參數傳遞必須要用Bundle或是Intent來把某個變數or物件傳給下一個Activity，這是比較一般的做法，不過還有另外一個繼承Applica&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1911/%e7%b9%bc%e6%89%bfapplication%e9%81%94%e5%88%b0global-variable/" data-wpel-link="internal">繼承Application達到Global Variable</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>(2012-02-04 更新)Android軟體要達到參數傳遞必須要用Bundle或是Intent來把某個變數or物件傳給下一個Activity，這是比較一般的做法，不過還有另外一個繼承Application的做法，讓某些變數變成全域變數，在每個Activity中可以直接取用。這篇文章將對如何繼承Application來做說明。<br />
<span id="more-1911"></span><br />
會使用繼承Application的原因其實在於前一段時間小蛙在串接行動廣告的時候遇到多執行緒的問題，由於原始做法是一個Activity跳到另一個Activity，每個Activity中讀取一次廣告，如此一來，在來回了幾次之後就會發生OOM的情況(不一定會發生，但速度會變得很慢，從DDMS中可以看到執行緒狂開)。<br />
小蛙得知繼承Application可以達到全域變數的功用後，異想天開的想把Google AdWhirl的AdWhirlLayout物件設成全域變數，只有第一次需要做初始化的動作，其他Activity只需要把這個變數拿回來用，如此一來就可以避免掉每個Activity都初始化一次造成的OOM。<br />
試了之後發現還真的可以！直接看code吧！只有一個要注意的地方就是這邊傳入了前景的Activity a，會傳進來的原因是KuBanner建立的時候必須傳入Activity當參數，如果不傳入前景的Activity當參數，會造成kuAD開啟後只有banner大小的視窗以及無法顯示網頁的問題。初始化的動作小蛙把它放在主程式執行時才呼叫。</p>
<pre class="brush: java; gutter: true">public class ApplicationBanner extends Application implements AdWhirlInterface{
    // Google AdWhirl Layout
    AdWhirlLayout adWhirlLayout;
    // 傳入主要(前景)的Activity
    Activity a;
    // kuAD banner
    KuBanner banner;
    // Google AdWhril Key
    private static final String ADWHIRL_KEY = "xxxxxxxxxxxxxxxx";
    // kuAD Key, or other keys( vpon … etc)
    private static final String KUAD_KEY        = "xxxxxxxxx";
    // 初始化 adWhirlLayout 供其他 Activity 直接取用
    public void initAdWhirlLayout(Activity a){
        this.a = a;
        adWhirlLayout = new AdWhirlLayout(a, ADWHIRL_KEY);
        AdWhirlManager.setConfigExpireTimeout(1000 * 60);
        AdWhirlTargeting.setAge(28);
        AdWhirlTargeting.setGender(AdWhirlTargeting.Gender.MALE);
        AdWhirlTargeting.setPostalCode("115");
        // 是否為測試模式，上架時要改成false
        AdWhirlTargeting.setTestMode(false);
        adWhirlLayout.setAdWhirlInterface(this);
    }
    // 在 Google AdWhirl 設定的 function
    public void showKuAD(){
        // 這邊要注意，如果這個 activity 一定要是前景視窗的
        banner = new KuBanner(a);
        banner.setAPID(KUAD_KEY);
        banner.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        // 這行改掉，換成 pushSubView
        //adWhirlLayout.handler.post(new ViewAdRunnable(adWhirlLayout, banner));
        adWhirlLayout.pushSubView(banner);
        banner.setkuADListener(new kuADListener() {
            public void onRecevie(String arg0) {
                adWhirlLayout.adWhirlManager.resetRollover();
                adWhirlLayout.rotateThreadedDelayed();
            }
            public void onFailedRecevie(String arg0) {
                banner.setkuADListener(null);
                adWhirlLayout.rollover();
            }
        });
    }
    // 印象中查到的資料這個不保證會執行，但還是放著，在軟體結束的時候，從 Activity 那邊刪程序比較保險些
    @Override
    public void onTerminate() {
        super.onTerminate();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}</pre>
<p>接著在AndroidManifest.xml中加入剛剛建立的Application名稱。加在原本的&lt;application&gt;標籤中。</p>
<pre class="brush: xml; gutter: true">&lt;application android:name="ApplicationBanner"
android:icon="@drawable/icon"
android:label="@string/app_name"&gt;
    &lt;activity&gt; … &lt;/activity&gt;
&lt;/application&gt;</pre>
<p>Layout中加入要放置廣告的區塊，例如小蛙的區塊設定如下：</p>
<pre class="brush: xml; gutter: true">&lt;LinearLayout
    android:id="@+id/banner"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"&gt;
&lt;/LinearLayout&gt;</pre>
<p>最後一個步驟了，在Activity中宣告並使用Application內的全域變數。這裡有一個要注意的部分，cleanLayout()的目的在於如果不先把原本layout中的東西清除，到下一個Activity又塞東西進去，會發生錯誤。在這邊小蛙想了很久到底什麼時候要清什麼時候要塞，後來發現只要在每個Activity　onPause的時候清空，onResume的時候塞回去就可以了，這邊可以思考一下有沒有更好的方法，但小蛙這樣用到現在沒有發生錯誤！</p>
<pre class="brush: java; gutter: true">ApplicationBanner     appState;
AdWhirlLayout        adWhirlLayou;
LinearLayout         layout;
//@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // 取得剛剛宣告的 Application
    appState = (ApplicationBanner)getApplication();
    // 呼叫內部方法初始化，只有第一個Activity需要初始化，其他Activity可以直接使用
    appState.initAdWhirlLayout(ClassName.this);
    // 取得 Google AdWhirl Layout 物件
    adWhirlLayou = appState.adWhirlLayout;
}
// 刪除 ad banner layout
public void cleanLayout(){
    if(layout != null){
        layout.removeAllViews();
    }
}
@Override
public void onPause() {
    cleanLayout();
    super.onPause();
}
@Override
public void onResume() {
    if(layout == null){
        layout = (LinearLayout)findViewById(R.id.banner);
    }
    if(layout != null){
        layout.removeAllViews();
        layout.addView(adWhirlLayou);
        layout.invalidate();
    }
    super.onResume();
}</pre>
<p>希望這篇文章對想在軟體中加入廣告，又不想大費周章把所有寫好的Activity改成Fragment的網友有幫助！<br />
2012-02-04<br />
如果忘了在AndroidManifest.xml加上<span style="color: #ff0000;"><strong>&lt;activity android:name=&#8221;com.kuad.ADDisplay&#8221;/&gt;</strong></span>也會造成點擊廣告之後只有banner大小的視窗。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1911/%e7%b9%bc%e6%89%bfapplication%e9%81%94%e5%88%b0global-variable/" data-wpel-link="internal">繼承Application達到Global Variable</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1911/%e7%b9%bc%e6%89%bfapplication%e9%81%94%e5%88%b0global-variable/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
