2013-04-26 85 views
0

我試圖在我的主要活動啓動時顯示隨機圖像5秒(我有3張圖像)。 (這是一種教程,如何使用我的應用程序和一些廣告)。但我只是想每天展示一次。我需要使用SharedPreferences嗎?這是最好的做法,是嗎?所以我發現這個:一旦活動第一次出現,如何顯示x秒的隨機圖像?

ImageView imgView = new ImageView(this); 
Random rand = new Random(); 
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1 
String imgName = "img" + rndInt; 
int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
imgView.setImageResource(id); 

要顯示一個隨機圖像。而這個:

public class mActivity extends Activity { 
@Overrride 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.id.layout); 

    // Get current version of the app 
    PackageInfo packageInfo = this.getPackageManager() 
     .getPackageInfo(getPackageName(), 0); 
    int version = packageInfo.versionCode; 

    SharedPreferences sharedPreferences = this.getPreferences(MODE_PRIVATE); 
    boolean shown = sharedPreferences.getBoolean("shown_" + version, false); 

    ImageView imageView = (ImageView) this.findViewById(R.id.newFeature); 
    if(!shown) { 
     imageView.setVisibility(View.VISIBLE); 

     // "New feature" has been shown, then store the value in preferences 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.put("shown_" + version, true); 
     editor.commit(); 
    } else 
     imageView.setVisibility(View.GONE); 
} 

要顯示一次應用程序更新應用程序的當前版本。我試圖將這些代碼適用於我的應用程序,但是我失敗了。我還需要圖像必須僅顯示5秒鐘並自動關閉。

嘿,這是我的一次。我現在得到這個代碼,它完美的作品:

boolean firstboot = getSharedPreferences("BOOT_PREF",MODE_PRIVATE).getBoolean("firstboot", true); 
    getSharedPreferences("BOOT_PREF",MODE_PRIVATE).edit(). 
    putBoolean("firstboot", true).commit(); 

if(firstboot){ 
Intent webBrowser = new Intent(getApplicationContext(), WebBrowser.class); 
// dismiss it after 5 seconds 
    webBrowser.putExtra("url", "http://sce.jelocalise.fr/mobile/ajax/interstitiel.php"); 
    startActivity(webBrowser); 

    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      Intent MyIntent = new Intent(getApplicationContext(), Home.class); 
      startActivity(MyIntent); 
      } 
     } 
    }, 5000); 

    getSharedPreferences("BOOT_PREF",MODE_PRIVATE).edit(). 
    putBoolean("firstboot", false).commit(); 
         } 

我現在想:有取消對我的WebView按鈕,當我點擊它,它完成了web瀏覽器的活動。問題是當我點擊取消按鈕處理程序不停止,並在5秒鐘後重新加載家庭活動(這是我知道的正常情況)。我只是想取消按鈕殺死處理程序。我試過handler.removeCallbacks方法,但我並不真正瞭解它是如何工作的。

+0

嗨,在谷歌搜索「閃屏」,我認爲這就是你要找的,你只需要檢查一個偏好,這將有最後一次顯示和今天的時間,看看它是否已經可用 – 2013-04-26 06:58:11

+0

這裏是你正在尋找http://stackoverflow.com/questions/15452061/android-splash-screen – 2013-04-26 07:00:24

+0

嗨thx爲您的幫助,但我已經有一個啓動畫面。我忘了說這個。這是我第三次活動(這是我的主要),我想展示我的一些解釋和廣告(因爲我得到了其他產品)。 – 2013-04-26 07:01:10

回答

0

好了,你要顯示的圖像爲5秒,你不想來顯示圖像往往比每天一次?這意味着您需要跟蹤最後一次顯示圖像的時間,SharedPreferences適用於此。我建議你使用自定義AlertDialog來顯示圖像。它看起來不錯,它會模糊背景中的活動。我建議使用Timer和TimerTask在一段時間後關閉對話框。這裏有一個例子:

/** 
* imageIds is an array of drawable resource id to chose from 
* Put the images you like to display in res/drawable-nodpi (if you 
* prefer to provide images for several dpi_bracket you put them in 
* res/drawable-mpdi, drawable-hdpi etc). 
* Add each of their resource ids to the array. In the example 
* below I assume there's four images named myimage1.png (or jpg), 
* myimage2, myimage3 and myimage4. 
*/ 
@Overrride 
public void onCreate(Bundle savedInstanceState) { 
    final int[] imageIds = { R.drawable.myimage1, R.drawable.myimage2, R.drawable.myimage3, R.drawable.myimage4 }; 
    final int id = new Random().nextInt(imageIds.length - 1); 
    showImageDialog(id, 24 * 60 * 60 * 1000, 5000); 
} 

/** 
* Show an image in a dialog for a certain amount of time before automatically dismissing it 
* The image will be shown no more frequently than a specified interval 
* @param drawableId A resource id for a drawable to show 
* @param minTime Minimum time in millis that has to pass since the last time an aimage was shown 
* @param delayBeforeDismiss Time in millis before dismissing the dialog 
* 
*/ 
private void showImageDialog(int drawableId, long minTime, long delayBeforeDismiss) { 
    final SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
    // make sure we don't show image too often 
    if((System.currentTimeMillis() - minTime) < prefs.getLong("TIMESTAMP", 0)) { 
     return; 
    } 

    // update timestamp 
    prefs.edit().putLong("TIMESTAMP", System.currentTimeMillis()).commit(); 

    // create a custom alert dialog with an imageview as it's only content 
    ImageView iv = new ImageView(this); 
    iv.setBackgroundDrawable(getResources().getDrawable(drawableId));  
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(iv); 
    final AlertDialog dialog = builder.create(); 
    dialog.show(); 

    // dismiss it after 5 seconds 
    new Timer().schedule(new TimerTask() {   
     @Override 
     public void run() { 
      dialog.dismiss(); 
     } 
    }, delayBeforeDismiss); 
} 
+0

Thx我會試試這個!只是爲了確保這段代碼不會選擇一個隨機圖像嗎? 還是,真的很大thx!我會適應Rstar的答案與你的結合「隨機」的事情和「一天一次」的事情。 – 2013-04-26 07:51:33

+0

正確。它沒有選擇隨機圖像。我會更新我的答案。 – britzl 2013-04-26 10:44:51

+0

順便說一句你選擇隨機圖像的部分代碼沒有問題。我在我的更新示例中保留了這一點。 – britzl 2013-04-26 10:52:28

1

試試這個代碼

public class MainActivity extends Activity { 

Random random = new Random(); 
int max = 2; 
int min = 0; 

ImageView imageView; 

Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android }; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    int randomNumber = random.nextInt(max - min + 1) + min; 

    imageView = (ImageView) findViewById(R.id.img); 

    imageView.setImageResource(image[randomNumber]); 

    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      Intent intent = new Intent(MainActivity.this, Act.class); 
      startActivity(intent); 
     } 
    }, 5000); 
    } 
} 
+0

好的,謝謝你Rstar,我會嘗試用britzl的代碼來調整你的代碼。 再次,Thx。 – 2013-04-26 07:54:11

相關問題