首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

通置market更新软件

2012-09-29 
通放market更新软件public class hello extends Activity {/** Called when the activity is first create

通放market更新软件
public class hello extends Activity {
/** Called when the activity is first created. */
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
SharedPreferences prefs = getPreferences(0);
long lastUpdateTime = prefs.getLong("lastUpdateTime", System.currentTimeMillis());
int curVersion = 0;
try {
curVersion = getPackageManager().getPackageInfo("linhai.com.hello", 0).versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("DEMO",String.valueOf(curVersion));
if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
lastUpdateTime = System.currentTimeMillis();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
// checkUpdate.start();
}

}
private Thread checkUpdate = new Thread(){
public void run() {
try {
URL updateURL = new URL("http://my.company.com/update");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}


final String s = new String(baf.toByteArray());
int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
int newVersion = Integer.valueOf(s);
if (newVersion > curVersion) {
mHandler.post(showUpdate);
}
} catch (Exception e) {
}
}
};


private Runnable showUpdate = new Runnable(){
public void run(){
new AlertDialog.Builder(hello.this).setIcon(R.drawable.ok).setTitle("Update Available")
.setMessage("An update for is available!\n\nOpen Android Market and see the details?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
startActivity(intent);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).show();
}
};

}

热点排行