我已经部署了我的应用程序并且正在使用中。每个月左右我都会更新它并添加新功能。我想仅在用户使用更新的应用时第一次显示“新”图像。我怎么才能只展示一次?我应该从哪里开始?
答案 0 :(得分:2)
也许这样的事情可能有助于解决您的问题:
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);
}
下次运行应用程序时,图像将不会显示。
答案 1 :(得分: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