Android 如何在没有xml的设置活动中实现admob广告

Android 如何在没有xml的设置活动中实现admob广告,android,admob,Android,Admob,我想在我的设置活动中放置admob广告,因为我不使用任何xml进行设置活动,我想在listview中放置广告 “我的设置”活动如下所示: public class SettingsActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, PhotoViewer.PhotoViewerProvider { private ListView listView; pr

我想在我的设置活动中放置admob广告,因为我不使用任何xml进行设置活动,我想在listview中放置广告

“我的设置”活动如下所示:

public class SettingsActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, PhotoViewer.PhotoViewerProvider {

    private ListView listView;
    private ListAdapter listAdapter;
    private BackupImageView avatarImage;
    private TextView nameTextView;
    private TextView onlineTextView;
    private ImageView writeButton;
    private AnimatorSetProxy writeButtonAnimation;
    private AvatarUpdater avatarUpdater = new AvatarUpdater();
    private View extraHeightView;
    private View shadowView;

    private int extraHeight;

    private int overscrollRow;
    private int emptyRow;
    private int numberSectionRow;
    private int numberRow;
    private int usernameRow;
    private int settingsSectionRow;
    private int settingsSectionRow2;
    private int enableAnimationsRow;
    private int notificationRow;
    private int backgroundRow;
    private int languageRow;
    private int privacyRow;
    private int mediaDownloadSection;
    private int mediaDownloadSection2;
    private int mobileDownloadRow;
    private int wifiDownloadRow;
    private int roamingDownloadRow;
    private int saveToGalleryRow;
    private int messagesSectionRow;
    private int messagesSectionRow2;
    private int customTabsRow;
    private int directShareRow;
    private int textSizeRow;
    private int stickersRow;
    private int cacheRow;
    private int raiseToSpeakRow;
    private int sendByEnterRow;
   //private int useSystemEmojiRow;
    private int supportSectionRow;
    private int supportSectionRow2;
    //  private int askQuestionRow;
    //private int telegramFaqRow;
    private int adsRow;
    private int privacyPolicyRow;
    private int sendLogsRow;
    private int clearLogsRow;
    private int switchBackendButtonRow;
    private int versionRow;
    private int contactsSectionRow;
    private int contactsReimportRow;
    private int contactsSortRow;
    private int autoplayGifsRow;
    private int rowCount;

    private final static int edit_name = 1;
    private final static int logout = 2;

    private static class LinkMovementMethodMy extends LinkMovementMethod {
        @Override
        public boolean onTouchEvent(@NonNull TextView widget, @NonNull Spannable buffer, @NonNull MotionEvent event) {
            try {
                return super.onTouchEvent(widget, buffer, event);
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            return false;
        }
    }

    @Override
    public boolean onFragmentCreate() {
        super.onFragmentCreate();
        avatarUpdater.parentFragment = this;
        avatarUpdater.delegate = new AvatarUpdater.AvatarUpdaterDelegate() {
            @Override
            public void didUploadedPhoto(TLRPC.InputFile file, TLRPC.PhotoSize small, TLRPC.PhotoSize big) {
                TLRPC.TL_photos_uploadProfilePhoto req = new TLRPC.TL_photos_uploadProfilePhoto();
                req.caption = "";
                req.crop = new TLRPC.TL_inputPhotoCropAuto();
                req.file = file;
                req.geo_point = new TLRPC.TL_inputGeoPointEmpty();
                ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                    @Override
                    public void run(TLObject response, TLRPC.TL_error error) {
                        if (error == null) {
                            TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId());
                            if (user == null) {
                                user = UserConfig.getCurrentUser();
                                if (user == null) {
                                    return;
                                }
                                MessagesController.getInstance().putUser(user, false);
                            } else {
                                UserConfig.setCurrentUser(user);
                            }
                            TLRPC.TL_photos_photo photo = (TLRPC.TL_photos_photo) response;
                            ArrayList<TLRPC.PhotoSize> sizes = photo.photo.sizes;
                            TLRPC.PhotoSize smallSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 100);
                            TLRPC.PhotoSize bigSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 1000);
                            user.photo = new TLRPC.TL_userProfilePhoto();
                            user.photo.photo_id = photo.photo.id;
                            if (smallSize != null) {
                                user.photo.photo_small = smallSize.location;
                            }
                            if (bigSize != null) {
                                user.photo.photo_big = bigSize.location;
                            } else if (smallSize != null) {
                                user.photo.photo_small = smallSize.location;
                            }
                            MessagesStorage.getInstance().clearUserPhotos(user.id);
                            ArrayList<TLRPC.User> users = new ArrayList<>();
                            users.add(user);
                            MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_ALL);
                                    NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
                                    UserConfig.saveConfig(true);
                                }
                            });
                        }
                    }
                });
            }
        };
        NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);

        rowCount = 0;
        overscrollRow = rowCount++;
        emptyRow = rowCount++;
        numberSectionRow = rowCount++;
        numberRow = rowCount++;
        usernameRow = rowCount++;
        settingsSectionRow = rowCount++;
        settingsSectionRow2 = rowCount++;
        notificationRow = rowCount++;
        privacyRow = rowCount++;
        backgroundRow = rowCount++;
        languageRow = rowCount++;
        adsRow =  rowCount++;
        enableAnimationsRow = rowCount++;
        mediaDownloadSection = rowCount++;
        mediaDownloadSection2 = rowCount++;
        mobileDownloadRow = rowCount++;
        wifiDownloadRow = rowCount++;
        roamingDownloadRow = rowCount++;
        if (Build.VERSION.SDK_INT >= 11) {
            autoplayGifsRow = rowCount++;
        }
        saveToGalleryRow = rowCount++;
        messagesSectionRow = rowCount++;
        messagesSectionRow2 = rowCount++;
        customTabsRow = rowCount++;
        if (Build.VERSION.SDK_INT >= 23) {
            directShareRow = rowCount++;
        }
        textSizeRow = rowCount++;
        stickersRow = rowCount++;
        cacheRow = rowCount++;
        raiseToSpeakRow = rowCount++;
        sendByEnterRow = rowCount++;
      // useSystemEmojiRow = rowCount++;
        supportSectionRow = rowCount++;
        supportSectionRow2 = rowCount++;
        //  askQuestionRow = rowCount++;
        //telegramFaqRow = rowCount++;
        privacyPolicyRow = rowCount++;
        if (BuildVars.DEBUG_VERSION) {
            sendLogsRow = rowCount++;
            clearLogsRow = rowCount++;
            switchBackendButtonRow = rowCount++;
        }
        versionRow = rowCount++;
        //contactsSectionRow = rowCount++;
        //contactsReimportRow = rowCount++;
        //contactsSortRow = rowCount++;

        MessagesController.getInstance().loadFullUser(UserConfig.getCurrentUser(), classGuid, true);

        return true;
    }

    @Override
    public void onFragmentDestroy() {
        super.onFragmentDestroy();
        if (avatarImage != null) {
            avatarImage.setImageDrawable(null);
        }
        MessagesController.getInstance().cancelLoadFullUser(UserConfig.getClientUserId());
        NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateInterfaces);
        avatarUpdater.clear();
    }

    @Override
    public View createView(Context context) {
        actionBar.setBackgroundColor(AvatarDrawable.getProfileBackColorForId(5));
        actionBar.setItemsBackground(AvatarDrawable.getButtonColorForId(5));
        actionBar.setBackButtonImage(R.drawable.ic_ab_back);
        actionBar.setAddToContainer(false);
        extraHeight = 88;
        if (AndroidUtilities.isTablet()) {
            actionBar.setOccupyStatusBar(false);
        }
        actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
            @Override
            public void onItemClick(int id) {
                if (id == -1) {
                    finishFragment();
                } else if (id == edit_name) {
                    presentFragment(new ChangeNameActivity());
                } else if (id == logout) {
                    if (getParentActivity() == null) {
                        return;
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setMessage(LocaleController.getString("AreYouSureLogout", R.string.AreYouSureLogout));
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            MessagesController.getInstance().performLogout(true);
                        }
                    });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    showDialog(builder.create());
                }
            }
        });
        ActionBarMenu menu = actionBar.createMenu();
        ActionBarMenuItem item = menu.addItem(0, R.drawable.ic_ab_other);
        item.addSubItem(edit_name, LocaleController.getString("EditName", R.string.EditName), 0);
        item.addSubItem(logout, LocaleController.getString("LogOut", R.string.LogOut), 0);

        listAdapter = new ListAdapter(context);

        fragmentView = new FrameLayout(context) {
            @Override
            protected boolean drawChild(@NonNull Canvas canvas, @NonNull View child, long drawingTime) {
                if (child == listView) {
                    boolean result = super.drawChild(canvas, child, drawingTime);
                    if (parentLayout != null) {
                        int actionBarHeight = 0;
                        int childCount = getChildCount();
                        for (int a = 0; a < childCount; a++) {
                            View view = getChildAt(a);
                            if (view == child) {
                                continue;
                            }
                            if (view instanceof ActionBar && view.getVisibility() == VISIBLE) {
                                if (((ActionBar) view).getCastShadows()) {
                                    actionBarHeight = view.getMeasuredHeight();
                                }
                                break;
                            }
                        }
                        parentLayout.drawHeaderShadow(canvas, actionBarHeight);
                    }
                    return result;
                } else {
                    return super.drawChild(canvas, child, drawingTime);
                }
            }
        };
        FrameLayout frameLayout = (FrameLayout) fragmentView;

        listView = new ListView(context);
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setVerticalScrollBarEnabled(false);
        AndroidUtilities.setListViewEdgeEffectColor(listView, AvatarDrawable.getProfileBackColorForId(5));
        frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT));
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                if (i == textSizeRow) {
                    if (getParentActivity() == null) {
                        return;
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle(LocaleController.getString("TextSize", R.string.TextSize));
                    final NumberPicker numberPicker = new NumberPicker(getParentActivity());
                    numberPicker.setMinValue(12);
                    numberPicker.setMaxValue(30);
                    numberPicker.setValue(MessagesController.getInstance().fontSize);
                    builder.setView(numberPicker);
                    builder.setNegativeButton(LocaleController.getString("Done", R.string.Done), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("fons_size", numberPicker.getValue());
                            MessagesController.getInstance().fontSize = numberPicker.getValue();
                            editor.commit();
                            if (listView != null) {
                                listView.invalidateViews();
                            }
                        }
                    });
                    showDialog(builder.create());
                } else if (i == enableAnimationsRow) {
                    SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
                    boolean animations = preferences.getBoolean("view_animations", true);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("view_animations", !animations);
                    editor.commit();
                    if (view instanceof TextCheckCell) {
                        ((TextCheckCell) view).setChecked(!animations);
                    }
                } else if (i == notificationRow) {
                    presentFragment(new NotificationsSettingsActivity());
                } else if (i == backgroundRow) {
                    presentFragment(new WallpapersActivity());
                }
                else if (i == adsRow) {


                }
公共类设置活动扩展BaseFragment实现NotificationCenter.NotificationCenterDelegate、PhotoViewer.PhotoViewerProvider{
私有列表视图列表视图;
私有ListAdapter ListAdapter;
私有备份图像视图;
私有文本视图名称文本视图;
私有TextView onlineTextView;
私有ImageView写入按钮;
私有AnimatorSetProxy writeButtonAnimation;
私有化身更新者化身更新者=新化身更新者();
私有视图超高视图;
私密视野;
私人室内超高;
斯克罗罗私人酒店;
私人int emptyRow;
私有int numberSectionRow;
私人国际号码;
私有int usernameRow;
私有int设置sectionrow;
私有int设置sectionRow2;
私有int enableAnimationsRow;
私人国际通知行;
私人int背景行;
私立国际语言学校;
私人私人住宅区;
私有int媒体下载部分;
私人int媒体下载第2节;
私有int移动下载行;
私有int wifiDownloadRow;
私人互联网漫游下载行;
私有int saveToGalleryRow;
私有int-messagesSectionRow;
私有int消息sectionrow2;
私人int customTabsRow;
私有int directShareRow;
私有int-textSizeRow;
私人住宅;
私人int缓存行;
私人葡萄牙语俱乐部;
私用int sendByEnterRow;
//私有int UseSystemoJirow;
私人国际贸易区;
私营部门第2行;
//私人国际机场跑道;
//私人int-ROW;
私人国际存托凭证;
private int privacyPolicyRow;
私人int sendLogsRow;
私人int clearLogsRow;
私人int switchBackendButtonRow;
私有int版本行;
私人int contactsSectionRow;
私人国际联络处;
私人int contactsSortRow;
私家车;
私有整数行数;
私有最终静态整数编辑_name=1;
私有最终静态int注销=2;
私有静态类LinkMovementMethodMy扩展LinkMovementMethod{
@凌驾
公共布尔onTouchEvent(@NonNull TextView小部件、@NonNull Spanable缓冲区、@NonNull MotionEvent事件){
试一试{
返回super.onTouchEvent(小部件、缓冲区、事件);
}捕获(例外e){
FileLog.e(“tmessages”,e);
}
返回false;
}
}
@凌驾
公共布尔值onFragmentCreate(){
super.onFragmentCreate();
avatarUpdater.parentFragment=此;
avatarUpdater.delegate=新的avatarUpdater.avatarupdaterregate(){
@凌驾
public void diduploatedphoto(TLRPC.InputFile文件,TLRPC.PhotoSize小,TLRPC.PhotoSize大){
TLRPC.TL_photos_uploadProfilePhoto req=新建TLRPC.TL_photos_uploadProfilePhoto();
req.caption=“”;
req.crop=new TLRPC.TL_inputPhotoCropAuto();
req.file=文件;
req.geo_point=new TLRPC.TL_inputGeoPointEmpty();
ConnectionsManager.getInstance().sendRequest(请求,新的RequestDelegate()){
@凌驾
公共无效运行(TLObject响应,TLRPC.TL_错误){
如果(错误==null){
TLRPC.User User=MessagesController.getInstance().getUser(UserConfig.getClientUserId());
if(user==null){
user=UserConfig.getCurrentUser();
if(user==null){
返回;
}
MessagesController.getInstance().putUser(用户,false);
}否则{
UserConfig.setCurrentUser(用户);
}
TLRPC.TL_photos_photo=(TLRPC.TL_photos_photo)响应;
ArrayList大小=photo.photo.size;
TLRPC.PhotoSize smallSize=FileLoader.getClosestPhotoSizeWithSize(大小,100);
TLRPC.PhotoSize bigSize=FileLoader.getClosestPhotoSizeWithSize(大小,1000);
user.photo=new TLRPC.TL_userProfilePhoto();
user.photo.photo_id=photo.photo.id;
if(smallSize!=null){
user.photo.photo_small=smallSize.location;
}
if(bigSize!=null){
user.photo.photo_big=bigSize.location;
}else if(smallSize!=null){
user.photo.photo_small=smallSize.location;
}
MessagesStorage.getInstance().clearUserPhotos(user.id);
ArrayList用户=新建ArrayList();
用户。添加(用户);
MessagesStorage.getInstance().putUsersAndChats(users,null,false,true);
AndroidUtilities.runOnUIThread(新的Runnable(){
@凌驾
公开募捐{
NotificationCenter.getInstance().postNotificationName(NotificationCenter.UpdateInterface,MessagesController.UPDATE\u MASK\u ALL);
NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
UserConfig.saveConfig(true);
     // Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
     AdView mAdView = new AdView(this);
     mAdView.setAdSize(AdSize.SMART_BANNER);
     mAdView.setAdUnitId("myAdUnitId");

     // Create an ad request.
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

     // Add the AdView to the view hierarchy.
     layout.addView(mAdView);

     // Start loading the ad.
     mAdView.loadAd(adRequestBuilder.build());