1

我已经部署了我的应用程序并且它正在使用中。每个月左右我都会更新它并添加新功能。我只想在用户第一次使用更新的应用程序时显示“新”图像。我怎样才能只显示一次?我应该从哪里开始?

4

2 回答 2

2

Maybe something like this may help to solve your problem:

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

The next time you run the application, the image will not show.

于 2012-04-09T17:41:24.393 回答
0

如果您有可用于存储图像的服务器,请将其放在那里并在更新时下载。

简单的方法是使用 sharedprefernece 来保存当前的应用程序版本,然后在它打开时对其进行检查。如果它返回版本与存储的版本不同,请运行您的图像下载器并以所需的方式显示它。:)

这是我在我的一个应用程序中使用的一个示例,将 ImageView 声明放在你的 oncreate 中,并在其中的某个地方给方法他们自己的位置,你的好:)

要记住的另一件事是,您可以使用图像托管程序(例如 imgur)为您的客户托管“应用程序更新”图像,并且由于您会定期更新,因此您不必担心图像仍然存在时会被删除使用(如果您保持应用程序更新)

    private final static String URL = "http://hookupcellular.com/wp-content/images/android-app-update.png";

    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(downloadImage(URL));


private Bitmap downloadImage(String IMG_URL)
{
    Bitmap bitmap = null;
    InputStream in = null;        
    try {
        in = OpenHttpConnection(IMG_URL);
        bitmap = BitmapFactory.decodeStream(in);
       in.close();
 } catch (IOException e1) {
   e1.printStackTrace();
 }
 return bitmap;                
 }

private static InputStream OpenHttpConnection(String urlString) throws IOException
{
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                     
        throw new IOException("Not an HTTP connection");

    try
    {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }                     
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting to " + URL);
    }
    return in;     
}

希望这会有所帮助:D

于 2012-04-09T17:06:51.967 回答