[英]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
}
}
编辑:已解决
这是我解决它的方法;
@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.