2017-10-29 20 views
-1

私は、JavaとAndroidの両方の開発ではなく、むしろ新しいです。私はAndroid StudioのGoogle AdMod広告アクティビティテンプレートの1つを使用しています。アプリが終了してレベル整数を保存して、再びバックアップを開始するときに呼び出すことができるようにしたいと考えています。私はSharedPreferencesを使って多くの例を見つけましたが、このプロジェクトでどのように使用するのか分かりません。それはおそらく単純です。ここでは、SharedPreferencesを使用する方法です終了時にintを保存するには?

package com.example.dthom.adsimulator; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.InterstitialAd; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
// Remove the below line after defining your own ad unit ID. 
private static final String TOAST_TEXT = "Test ads are being shown. " 
     + "To show live ads, replace the ad unit ID in 
res/values/strings.xml with your own ad unit ID."; 

private static final int START_LEVEL = 1; 
private int mLevel; 
private Button mNextLevelButton; 
private InterstitialAd mInterstitialAd; 
private TextView mLevelTextView; 

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

    // Create the next level button, which tries to show an interstitial 
when clicked. 
    mNextLevelButton = ((Button) findViewById(R.id.next_level_button)); 
    mNextLevelButton.setEnabled(false); 
    mNextLevelButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showInterstitial(); 
     } 
    }); 

    // Create the text view to show the level number. 
    mLevelTextView = (TextView) findViewById(R.id.level); 
    mLevel = START_LEVEL; 

    // Create the InterstitialAd and set the adUnitId (defined in 
values/strings.xml). 
    mInterstitialAd = newInterstitialAd(); 
    loadInterstitial(); 

    // Toasts the test ad message on the screen. Remove this after defining 
your own ad unit ID. 
    Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

private InterstitialAd newInterstitialAd() { 
    InterstitialAd interstitialAd = new InterstitialAd(this); 
    interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 
    interstitialAd.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      mNextLevelButton.setEnabled(true); 
     } 

     @Override 
     public void onAdFailedToLoad(int errorCode) { 
      mNextLevelButton.setEnabled(true); 
     } 

     @Override 
     public void onAdClosed() { 
      // Proceed to the next level. 
      goToNextLevel(); 
     } 
    }); 
    return interstitialAd; 
} 

private void showInterstitial() { 
    // Show the ad if it's ready. Otherwise toast and reload the ad. 
    if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { 
     mInterstitialAd.show(); 
    } else { 
     Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); 
     goToNextLevel(); 
    } 
} 

private void loadInterstitial() { 
    // Disable the next level button and load the ad. 
    mNextLevelButton.setEnabled(false); 
    AdRequest adRequest = new AdRequest.Builder() 
      .setRequestAgent("android_studio:ad_template").build(); 
    mInterstitialAd.loadAd(adRequest); 
} 

private void goToNextLevel() { 
    // Show the next level and reload the ad to prepare for the level after. 
    mLevelTextView.setText("Level " + (++mLevel)); 
    mInterstitialAd = newInterstitialAd(); 
    loadInterstitial(); 
} 
} 

任意のヘルプは

+0

アンドロイドのドキュメントによると、それはあなたがリソースを解放すべきであると言いますonStopメソッドの他のシャットダウン操作、すなわち @Override protected void onStop(){ //スーパークラスメソッドを最初に呼び出す super.onStop(); //あなたの州のsavePreferencesメソッドここに } 詳細をチェックしてください。 https://developer.android.com/guide/components/activities/activity-lifecycle.html 、ここsharedPrefrencesを使用して: https://developer.android.com/training/basics/data-storage/shared -preferences.html – user606669

+0

GET: コンテキストコンテキスト= getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( "preferenceKeyNameHere"、コンテキスト。MODE_PRIVATE); ------------------------------ PUT: SharedPreferences sharedPref = getActivity()。getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt( "preferenceKeyNameHere"、yourNumber); editor.commit(); アイテムを格納するために使用するキーは、アイテムを取得するために使用します – user606669

答えて

0

まず、あなたはSharedPreferencesのインスタンスを取得したい:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 

今、あなたが番号を読み取ることができます。あなたが何かを保存したことがない場合、デフォルトの数字が1になります。レベルはその整数の名前になります:

Integer level = sharedPref.getInt("level",1); 

もそれを保存するには、エディタのインスタンスを取得し、新しいレベルに配置する必要があります。その後、commit()またはapply()のいずれかを使用する必要があります。 Commit()は、プログラムが次のコマンドを実行する前にレベルが保存されることを保証します。適用はバックグラウンドで保存されます。再びsharedPreferencesのインスタンスが必要です。あなたは新しいものを作るか、古いものを使うことができます。

SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putInt("level",newlevel); 
    editor.commit(); 

あなたはユーザー出口はonStop(でそれを行う際にレベルを設定し)、その後、あなたがそれを必要なときに再びそれを読みたい場合は、次の

@Override 
protected void onStop() { 
    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putInt("level",newlevel); 
    editor.commit(); 
    super.onStop(); 
} 
0

歓迎です:

は、ここに私のコードです。

  1. SharedPreferencesのインスタンスを取得します。

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE); 
    
  2. 保存int

    preferences.edit().putInt("level", value).commit(); 
    
  3. (デフォルトレベルで0を置き換えることができます)intを取得します。

    preferences.getInt("level", 0); 
    
関連する問題