0

当我的主要活动启动时,我试图显示一个随机图像 5 秒(我有 3 个图像)。(这是一种如何使用我的应用程序和一些广告的教程)。但我只想每天显示一次。我需要使用 SharedPreferences 对吗?这是最好的方法,不是吗?所以我发现了这个:

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 上有一个取消按钮,当我点击它时,它会完成 webBrowser 活动。问题是当我单击取消按钮时,处理程序不会停止,并且在 5 秒后它会重新加载 Home 活动(我知道这是正常的)。我只希望取消按钮杀死处理程序。我已经尝试过 handler.removeCallbacks 方法,但我并不真正了解它是如何工作的。

4

2 回答 2

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);
  }
}
于 2013-04-26T07:15:44.527 回答
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);
}
于 2013-04-26T07:45:53.323 回答