以下是我的代码:

package com.admobsdk_dfp_handler;

import com.google.ads.*;
import com.google.ads.doubleclick.*;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;

public class AdMobSDK_DFP_Handler extends Activity {
    private DfpAdView adView;
    private static final String adUnitId = "/7732/test_portal7/android_app1_test_portal7/top_banner_non_sdk_application_android_app1_test_portal7";
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {

        public void run() {
            adView.loadAd(new AdRequest());
            handler.postDelayed(this, 5000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);

        adView = new DfpAdView(this, AdSize.BANNER, adUnitId);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);

        layout.addView(adView);

        adView.loadAd(new AdRequest());

    };

    @Override
    protected void onPause() {
        handler.removeCallbacks(runnable);
        super.onPause();
    }

    @Override
    protected void onResume() {
        handler.postDelayed(runnable, 5000);
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }

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

}

protected void ondestroy()用于清理程序。但是,我看到了一些示例项目,它们通常使用public void ondestroy()来进行清理。
正如我所知道的,在Java中,可以在包内访问受保护的方法,并且可以在任何地方访问公共方法。但是通常一个类有自己的ondestroy(),那么这是否意味着可以使用其中的一个呢?在两者之间选择时,我有什么需要考虑的?
谢谢你的帮助。

最佳答案

在public和protected之间,功能没有区别。唯一的区别是,使用public,您可以对来自任何类的活动实例调用onDestroy()。使用protected,您只能在同一个包中的同一个类或子类中调用。对于onDestroy()我觉得公开它没什么意义。这实际上可能是一种不好的做法。
换言之,只要使用protected,除非您有一个非常特殊的原因使它可以被更多的类访问。
我认为你看到的公共实现只是一个错误/没有必要。如果你想测试,就把它改回protected。如果没有编译器错误,就没有必要。

10-08 03:06