簡體   English   中英

如何在 android 工作室的操作欄中添加刪除廣告按鈕?

[英]How to add remove ads button in action bar in android studio?

所以,基本上,我想在我的操作欄中添加一個刪除廣告按鈕,點擊它會在 Play 商店中打開我的應用程序的付費版本。

我已經做了很多設計,但不知道如何在代碼中實現它。

操作欄的設計

我會感謝任何能教我如何做的人。

這是我的代碼;

公共 class MainActivity 擴展 AppCompatActivity 實現 MyAsync.MyAsyncInterface,SwipeRefreshLayout.OnRefreshListener {

// Log
private final String TAG = getClass().getSimpleName();

// Constants
public static final String EXTRA_DISABLE_NOTIFICATION = "disable_notification";

// Layout
private View rootView;
private MySwipeRefreshLayout swipeRefreshLayout;
private InfoView infoView;

// Catalog
private CatalogAdapter catalogAdapter;
private Catalog catalog;

// Broadcast listener
private final IntentFilter broadcastFilter = new IntentFilter(SyncManager.ACTION_SYNC);
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            int result = intent.getIntExtra(SyncManager.EXTRA_RESULT, SyncManager.RESULT_FAIL);
            swipeRefreshLayout.setRefreshing(false); // Stop refresh layout
            if (result == SyncManager.RESULT_SUCCESS) {
                // Reload local content
                if (loadLocalContent()) {
                    refreshList();
                } else {
                    // This error is very unlikely to happen
                    showCatalogDownloadError();
                }
            } else if (result == SyncManager.RESULT_TIMEOUT) {
                // Connection timeout
                showTimeoutError();
            } else {
                // Catalog sync failed
                showCatalogDownloadError(); // Show catalog download error
            }
        }
    }
};

private Context context;
private Uri imageUri;

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

    context = this;

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Layout
    rootView = findViewById(R.id.main_container);
    GridView catalogList = findViewById(R.id.catalog_grid);
    swipeRefreshLayout = (MySwipeRefreshLayout) rootView;
    infoView = findViewById(R.id.info_view);

    // Swipe down refresh
    swipeRefreshLayout.setOnRefreshListener(this);
    swipeRefreshLayout.setSwipeableChildren(R.id.catalog_grid);

    // Catalog list
    catalogAdapter = new CatalogAdapter(this);
    catalog = new Catalog();
    catalogList.setAdapter(catalogAdapter);
    catalogList.setOnItemClickListener(catalogItemClickListener);

    // Before anything check if the sensors are available
    boolean checkSensors = sharedPreferences.getBoolean(PREF_CHECKSENS, PREF_CHECKSENS_DEFAULT);
    if (!Utils.sensorsAvailable(this) && checkSensors) {
        new AlertDialog.Builder(context)
                .setTitle(R.string.main_dialog_nosensor_title)
                .setMessage(R.string.main_dialog_nosensor_message)
                .setNegativeButton(R.string.common_ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sharedPreferences.edit().putBoolean(PREF_CHECKSENS, false).apply();
                        dialog.dismiss();
                    }
                })
                .show();
    }

    infoView.show(R.string.main_info_empty_title, R.string.main_info_empty_message);
}

@Override
protected void onResume() {
    super.onResume();

    // Register broadcast listener
    registerReceiver(broadcastReceiver, broadcastFilter);

    // Check if refresh is running without service
    if (swipeRefreshLayout.isRefreshing() && !SyncManager.isRunning()) {
        swipeRefreshLayout.setRefreshing(false);
    }

    // Load catalog if available
    loadLocalContent();
    refreshList();

    // Check if catalog is expired
    InternalData internalData = new InternalData(this);
    long lastTimestamp = internalData.readLong(LD_TIMESTAMP, 0);
    long delta = getTimestamp() - lastTimestamp;

    if (delta < 0 || delta > T_CATALOG_EXPIRATION || catalog.size() == 0) {
        // Catalog has expired download
        Log.d(TAG, "Catalog has reached expiration");
        loadRemoteContent();
    } else {
        Log.d(TAG, "Catalog is still valid!");
    }
}


@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(broadcastReceiver);
}

// Download Listener
@Override
public void onCompleted(int id, Bundle extra) {
    if (id == WallpaperDownloader.ID) {
        CatalogItem downloadedItem = extra.getParcelable(WallpaperDownloader.EXTRA_CATALOG_ITEM);
        startSetActivity(downloadedItem);
        refreshList(); // refresh list for downloaded icon
    }
}

編輯:已解決

這是我解決它的方法;

我剛剛做了一個新的布局和活動,並添加了一個按鈕,其中包含指向 Play 商店中付費版本應用程序的鏈接。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remove_ads);
}
public void premium (View view){
    openUrl("https://play.google.com/store/apps/details?id=com.example.app");
}
public void openUrl(String url)
{
    Uri uri=Uri.parse(url);
    Intent launchWeb=new Intent(Intent.ACTION_VIEW,uri);
    startActivity(launchWeb);
}

這是我制作的布局;

在此處輸入圖像描述

如果需要,我可以提供所有可繪制對象和代碼。 希望這可以幫助像我一樣在 Android 開發中的其他新人:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM