如何显示X秒随机图像,一旦活动是第一次提出了? [英] How to display a random image for x second once the activity is first brought up?

查看:103
本文介绍了如何显示X秒随机图像,一旦活动是第一次提出了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个随机图像5秒(我有3图像)时,我的主要活动将启动。 (这是一种教程如何使用我的应用程序和一些广告)。但我只是想每天一次显示它。我需要正确的使用共享preferences?这是做的最好的办法,是吗?所以,我发现这一点:

I'm trying to display a random image for 5 seconds (I've got 3 images) when my main activity is launched. (It's a kind of tutorial how to use my app and some advertisement). But I just want to display it once a day. I need to use SharedPreferences right? It's the best approach to do it, is it? So I found this:

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); 

要显示一个随机图像。而这个:

To display a random image. And this:

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);
}

要显示一旦应用程序更新应用程序的当前版本。我试着去适应那些codeS为我的应用程序,但我失败了。我还需要图像具有要显示只有5秒,自动关闭。

To show once an app is updated the current version of the app. I tried to adapt those codes for my app but I failed. I also need that the image has to be displayed just 5 seconds and close automatically.

嘿,这又是我。我现在得到这个code和它完美的作品:

Hey it's me again. I got this code now and it works perfectly:

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方法,但我并没有真正了解它是如何工作的。

What I want now: there is a cancel button on my webview and when I click on it, it finishes the webBrowser activity. The problem is when I click on the cancel button the handler doesn't stop, and after 5 seconds it reloads the Home activity (which is normal I know). I just want that the cancel button kills the handler. I've tried handler.removeCallbacks method but I didn't really understand how it works.

推荐答案

好了,你要显示的图像为5秒,你不想往往比每天一次来显示图像?这意味着,你需要保持您上次显示图像的轨道,共享preferences很适合这一点。我建议你​​使用自定义AlertDialog来显示图像。它看起来不错,它会暗淡了在后台活动。我建议你​​使用一个计时器,一个TimerTask的一段时间后,关闭该对话框。这里有一个例子:

Ok, so you want to show an image for 5 seconds and you don't want to show the image more often than once a day? This implies that you need to keep track of when you showed the image last, SharedPreferences works well for this. I suggest that you use a custom AlertDialog to show the image. It will look nice and it will dim out the activity in the background. And I'd recommend the use of a Timer and a TimerTask to dismiss the dialog after a certain period of time. Here's an example:

 /**
 * 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);
}

这篇关于如何显示X秒随机图像,一旦活动是第一次提出了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆