2013-04-26 29 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

相关问题