我是Android编程的新手。我创建了一个APP,在其中将视频添加到奖品中,这些视频为我提供了硬币奖品,该硬币奖品以TextView
的百分比显示。
问题是当我离开APP并返回或打开APP屏幕时,TextView
百分比计数器将重置为零,然后删除每个用户的工作。
我需要找到一种我无法编写的方法,以保存赚取的信用额度,最高可达100%,因此信用额度必须一直保留到最后,而不能删除它们,我尝试了onSaveInstanceState
过时的方法,但这种方法不起作用。
下面是我编写的应用代码。
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private AdView mAdView;
private WebView mywebview;
private InterstitialAd mInterstitialAd;
private FirebaseAnalytics mFirebaseAnalytics;
private RewardedVideoAd mRewardedVideoAd;
private static final String TAG = MainActivity.class.getName();
public double Coins;
RelativeLayout Home;
RelativeLayout PgCoins;
Button btn1, btn2;
ImageButton Backbtn;
TextView mCoinscounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "");
//counter CRD
mCoinscounter = (TextView) findViewById(R.id.Textcoins);
// WEbView
WebView mywebview = findViewById(R.id.mywebview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mywebview.setWebChromeClient(new WebChromeClient());
mywebview.getSettings().setUseWideViewPort(true);
mywebview.setFocusableInTouchMode(true);
mywebview.setWebViewClient(new WebViewClient());
mywebview.loadUrl("url");
Home = findViewById(R.id.home);
PgCoins = findViewById(R.id.pagecoins);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
Backbtn = findViewById(R.id.backbtn);
Home.setVisibility(View.VISIBLE);
PgCoins.setVisibility(View.GONE);
findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
// ADMob Top
mAdView = findViewById(R.id.adViewTop);
AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
mAdView.loadAd(adRequest);
// ADMob Bot
mAdView = findViewById(R.id.adViewBot);
AdRequest adRequest1 = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
mAdView.loadAd(adRequest1);
// ADMob Top Coins
mAdView = findViewById(R.id.adViewTopcoins);
AdRequest adRequest2 = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
mAdView.loadAd(adRequest2);
// ADMob Bot Coins
mAdView = findViewById(R.id.adViewBotcoins);
AdRequest adRequest3 = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
mAdView.loadAd(adRequest3);
//ADMob Interstitial
prepareAd();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "Interstitial not loaded");
}
prepareAd();
}
});
}
}, 60, 60, TimeUnit.SECONDS);
// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
//ADMob Video
loadRewardedVideoAd();
}
public void displayCrd (double amount){
mCoinscounter.setText(String.format("%.2f", amount));
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
//ADMob Interstitial
public void prepareAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
//Screen Change Page
boolean Change = true;
public void change(View view)
{
if (Change){
Home.setVisibility(View.GONE);
PgCoins.setVisibility(View.VISIBLE);
Change = false;
}else{
Home.setVisibility(View.VISIBLE);
PgCoins.setVisibility(View.GONE);
Change = true;
}
}
@Override
public void onRewardedVideoAdLoaded() {
Log.d(TAG, "Video Caricato");
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(this, " Amount " + rewardItem.getType() + " : " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
Coins += rewardItem.getAmount();
displayCrd(Coins/3600*100);
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Log.d(TAG, "Caricamento Fallito");
}
@Override
public void onRewardedVideoCompleted() {
loadRewardedVideoAd();
}
}
答案 0 :(得分:0)
您可以使用共享首选项。基本上,您可以将数据另存为键值。有关更多信息,请访问https://developer.android.com/training/data-storage/shared-preferences
写入Kotlin的共享首选项
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putInt(getString(R.string.saved_high_score_key), newHighScore)
commit()
}
从Kotlin的共享首选项中读取
val sharedPref = activity?.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE)
您必须做的是,将数据保存在“目的地”上,并读取数据在“创建”上。
答案 1 :(得分:0)
您必须使用SharedPreferences
因为您可以存储所需的值并进行检索。
使用此功能,您可以将价值存储在Preferredeces中
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("scrore", 10);
editor.putString("userName", "xyz");
editor.apply();
使用此方法,您可以从首选项中检索值
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String userName = prefs.getString("userName", null);
if (userName != null) {
int scrore = prefs.getInt("scrore", 0);//0 is the default value.
}
https://developer.android.com/reference/android/content/SharedPreferences
答案 2 :(得分:0)
您可以使用ViewModel代替onSaveInstanceState。它是Android的新组件,并且了解活动生命周期。
在这里简要阅读
https://developer.android.com/topic/libraries/architecture/viewmodel
如果要永久存储数据,则应使用移动数据库。 您可以使用Realm DB来存储您的详细信息及其非常简单的信息。
您可以在此处找到Realm示例