gpt4 book ai didi

android - 无法在 Android 应用程序中显示 Mopub 的原生广告

转载 作者:行者123 更新时间:2023-11-29 14:49:28 25 4
gpt4 key购买 nike

我已经与 Mopub 集成,以便在我的 Android 应用程序中显示广告。我可以展示横幅广告和插页式广告,但不能展示原生广告,

布局文件代码,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/note_title_label"
style="?android:listSeparatorTextViewStyle"
/>
<EditText android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/note_title_hint"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/note_details_label"
style="?android:listSeparatorTextViewStyle"
/>
<EditText android:id="@+id/note_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/note_details_hint"
/>
<Button android:id="@+id/note_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<CheckBox android:id="@+id/note_solved"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/note_solved_label"
/>

<RelativeLayout android:id="@+id/native_ad_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp">
>
<ImageView android:id="@+id/native_ad_main_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView android:id="@+id/native_ad_icon_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/native_ad_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/native_ad_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>

</LinearLayout>

fragment.java(与上述布局相关)文件中展示原生广告的逻辑,

    @TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_note, parent, false);



if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (NavUtils.getParentActivityName(getActivity()) != null) {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
mTitleField = (EditText)v.findViewById(R.id.note_title);
mTitleField.setText(mNote.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before, int count)
{

String title=Character.toUpperCase(c.toString().charAt(0)) + c.toString().substring(1);
mNote.setTitle(title);
}
public void beforeTextChanged(
CharSequence c, int start, int count, int after)
{// This space intentionally left blank
}
public void afterTextChanged(Editable c) {
// This one too
}
});
mDetailsField = (EditText)v.findViewById(R.id.note_details);
mDetailsField.setText(mNote.getDetails());
mDetailsField.addTextChangedListener(new TextWatcher() {
public void onTextChanged(
CharSequence c, int start, int before, int count)
{
mNote.setDetails(c.toString());
}
public void beforeTextChanged(
CharSequence c, int start, int count, int after)
{// This space intentionally left blank
}
public void afterTextChanged(Editable c) {
// This one too
}
});


mDateButton = (Button)v.findViewById(R.id.note_date);
mDateButton.setText(mNote.getDate().toString());
//mDateButton.setEnabled(false);
mDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
//DatePickerFragment dialog = new DatePickerFragment();
DatePickerFragment dialog = DatePickerFragment
.newInstance(mNote.getDate());
dialog.setTargetFragment(NoteFragment.this, REQUEST_DATE);
dialog.show(fm, DIALOG_DATE);
}
});

mSolvedCheckBox = (CheckBox)v.findViewById(R.id.note_solved);
mSolvedCheckBox.setChecked(mNote.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set the note's solved property
mNote.setSolved(isChecked);
}
});
/* Button reportButton = (Button)v.findViewById(R.id.note_reportButton);
reportButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("vnd.android-dir/mms-sms");
i.putExtra(Intent.EXTRA_TEXT, getNoteReport());
i.putExtra(Intent.EXTRA_SUBJECT,
mNote.getTitle());

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", "test man text");
startActivity(sendIntent);
}
});*/

//advertising start
MoPubNative.MoPubNativeListener moPubNativeListener = new MoPubNative.MoPubNativeListener() {
@Override
public void onNativeLoad(NativeResponse nativeResponse) {
// ...
}

@Override
public void onNativeFail(NativeErrorCode errorCode) {
// ...
}

@Override
public void onNativeImpression(View view) {
// ...
}

@Override
public void onNativeClick(View view) {
// ...
};
};

MoPubNative moPubNative = new MoPubNative(this.getActivity().getApplicationContext(), "248504aaf06d4a759a59d0615a5bdb54", moPubNativeListener);

Location exampleLocation = new Location("com.genedevelopers.android.yournote");
exampleLocation.setLatitude(23.1);
exampleLocation.setLongitude(42.1);
exampleLocation.setAccuracy(100);

RequestParameters requestParameters = new RequestParameters.Builder()
.keywords("gender:m,age:27")
.location(exampleLocation)
.build();
moPubNative.makeRequest(requestParameters);

ViewBinder viewBinder = new ViewBinder.Builder(R.layout.list_item_note)
.mainImageId(R.id.native_ad_main_image)
.iconImageId(R.id.native_ad_icon_image)
.titleId(R.id.native_ad_title)
.textId(R.id.native_ad_text)
.build();
//advertising end
return v;
}

所以请有人告诉我代码中有什么问题,或者如果有人有关于如何将 mopub 原生广告集成到 android 应用程序的示例代码,请分享它....

最佳答案

这是我用来展示原生广告的代码

  public class NativeAdActivity extends Activity {

private MoPubAdAdapter mAdAdapter;
private static final String MY_AD_UNIT_ID_PHONE = "xxxxxx";
private static final String MY_AD_UNIT_ID_TAB = "xxxxxx";
String screenSize;


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

// Set location awareness and precision globally
MoPub.setLocationAwareness(MoPub.LocationAwareness.TRUNCATED);
MoPub.setLocationPrecision(4);

// variable to grab the screenSize form resources
screenSize = getResources().getString(R.string.screen_type);


final ListView listView = (ListView) findViewById(R.id.native_list_view);


final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
for (int i = 0; i < 100; ++i) {
adapter.add("Item " + i);
}

// Create an ad adapter with ads in positions 0, 4, and every 10 places thereafter.
// This adapter will be used in place of the original adapter for the ListView.
mAdAdapter = new MoPubAdAdapter(this, adapter, MoPubNativeAdPositioning.clientPositioning()
.addFixedPosition(0)
.addFixedPosition(4)
.enableRepeatingPositions(10));

// Set up an renderer that knows how to put ad data in an ad view.
final MoPubNativeAdRenderer adRenderer = new MoPubNativeAdRenderer(
new ViewBinder.Builder(R.layout.native_ad_layout)
.titleId(R.id.native_ad_title)
.textId(R.id.native_ad_text)
.mainImageId(R.id.native_ad_main_image)
.iconImageId(R.id.native_ad_icon_image)
.build());

// Register the renderer with the MoPubAdAdapter and then set the adapter on the ListView.
mAdAdapter.registerAdRenderer(adRenderer);
listView.setAdapter(mAdAdapter);

}
@Override
public void onResume() {
// MoPub recommends loading knew ads when the user returns to your activity.
if(screenSize.equalsIgnoreCase("phone"))
{
mAdAdapter.loadAds(MY_AD_UNIT_ID_PHONE);
}else if(screenSize.equalsIgnoreCase("7-inch-tablet"))
{
mAdAdapter.loadAds(MY_AD_UNIT_ID_TAB);

}else if(screenSize.equalsIgnoreCase("10-inch-tablet") )
{
mAdAdapter.loadAds(MY_AD_UNIT_ID_TAB);
}
else //deafault case for phone
{
mAdAdapter.loadAds(MY_AD_UNIT_ID_PHONE);
}
super.onResume();
}


}

关于android - 无法在 Android 应用程序中显示 Mopub 的原生广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com