Android仅在第一时间显示“新”图像
问题描述:
我部署了我的应用程序并正在使用中。我每个月都会更新它并添加新功能。我想仅在用户使用更新的应用程序时第一次显示“新”图像。我怎么能只显示一次?我应该从哪里开始?Android仅在第一时间显示“新”图像
答
也许这样的事情可以帮助您解决问题:
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);
}
你运行应用程序,在下一次的图像将不会显示。
答
如果您有可用于存储图像的服务器,请将其放置并在更新时下载。
简单的方法是使用sharedprefernece来保存当前的应用程序版本,然后每当它打开时执行检查。如果它返回的版本与存储的版本不同,则运行图像下载器并以所需的方式显示它。 :)
下面有一个例子,我在我的应用程序之一使用,将ImageView的声明在你的OnCreate和地方给予方法有thier自己的斑点,你的好:)
一两件事要记住的是您可以使用图像托管服务器(例如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
+0
好想法。如何以编程方式在应用程序中执行此操作?截至目前,我无法访问服务器。 – user1143989 2012-04-09 17:13:37
作品!谢谢!!! – user1143989 2012-04-09 18:43:22