嗨,大家好我刚刚实现了开源项目自动更新apk ..因为我的应用程序不在Play商店,它变得非常愚蠢等待人们下载新的更新..我没有问题让它工作它是非常直接的实现..我的问题是他们给你一个免费帐户的数据津贴数量...我已经在不到一周的时间里使用了我所有的月份报价..想知道的是我如何更改代码所以它检查我所选择的网址而不是theres ..我读过它会涉及一个.txt文件,我的应用程序会在里面读取新版本号。
有关在自动更新apk类中更改内容的任何想法,以实现此目的吗?还有什么txt文件需要包含。谢谢你们的女孩们
这是auto updateapk项目的主要代码
public class AutoUpdateApk extends Observable {
// this class is supposed to be instantiated in any of your activities or,
// better yet, in Application subclass. Something along the lines of:
//
// private AutoUpdateApk aua; <-- you need to add this line of code
//
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
//
// aua = new AutoUpdateApk(getApplicationContext()); <-- and add this line too
//
public AutoUpdateApk(Context ctx) {
setupVariables(ctx);
}
// set icon for notification popup (default = application icon)
//
public static void setIcon( int icon ) {
appIcon = icon;
}
// set name to display in notification popup (default = application label)
//
public static void setName( String name ) {
appName = name;
}
// set update interval (in milliseconds)
//
// there are nice constants in this file: MINUTES, HOURS, DAYS
// you may use them to specify update interval like: 5 * DAYS
//
// please, don't specify update interval below 1 hour, this might
// be considered annoying behaviour and result in service suspension
//
public void setUpdateInterval(long interval) {
if( interval > 60 * MINUTES ) {
UPDATE_INTERVAL = interval;
} else {
Log_e(TAG, "update interval is too short (less than 1 hour)");
}
}
// software updates will use WiFi/Ethernet only (default mode)
//
public static void disableMobileUpdates() {
mobile_updates = false;
}
// software updates will use any internet connection, including mobile
// might be a good idea to have 'unlimited' plan on your 3.75G connection
//
public static void enableMobileUpdates() {
mobile_updates = true;
}
// call this if you want to perform update on demand
// (checking for updates more often than once an hour is not recommended
// and polling server every few minutes might be a reason for suspension)
//
public void checkUpdatesManually() {
checkUpdates(true); // force update check
}
public static final String AUTOUPDATE_CHECKING = "autoupdate_checking";
public static final String AUTOUPDATE_NO_UPDATE = "autoupdate_no_update";
public static final String AUTOUPDATE_GOT_UPDATE = "autoupdate_got_update";
public static final String AUTOUPDATE_HAVE_UPDATE = "autoupdate_have_update";
public void clearSchedule() {
schedule.clear();
}
public void addSchedule(int start, int end) {
schedule.add(new ScheduleEntry(start,end));
}
//
// ---------- everything below this line is private and does not belong to the public API ----------
//
protected final static String TAG = "AutoUpdateApk";
private final static String ANDROID_PACKAGE = "application/vnd.android.package-archive";
// private final static String API_URL = "http://auto-update-apk.appspot.com/check";
private final static String API_URL = "http://www.auto-update-apk.com/check";
protected static Context context = null;
protected static SharedPreferences preferences;
private final static String LAST_UPDATE_KEY = "last_update";
private static long last_update = 0;
private static int appIcon = R.mipmap.ic_launcher;
private static int versionCode = 0; // as low as it gets
private static String packageName;
private static String appName;
private static int device_id;
public static final long MINUTES = 60 * 1000;
public static final long HOURS = 60 * MINUTES;
public static final long DAYS = 24 * HOURS;
// 3-4 hours in dev.mode, 1-2 days for stable releases
private static long UPDATE_INTERVAL = 48 * DAYS; // how often to check
private static boolean mobile_updates = false; // download updates over wifi only
private final static Handler updateHandler = new Handler();
protected final static String UPDATE_FILE = "update_file";
protected final static String SILENT_FAILED = "silent_failed";
private final static String MD5_TIME = "md5_time";
private final static String MD5_KEY = "md5";
private static int NOTIFICATION_ID = 0xBEEF;
private static long WAKEUP_INTERVAL = 15 * MINUTES;
private class ScheduleEntry {
public int start;
public int end;
public ScheduleEntry(int start, int end) {
this.start = start;
this.end = end;
}
}
private static ArrayList<ScheduleEntry> schedule = new
ArrayList<ScheduleEntry>();
private Runnable periodicUpdate = new Runnable() {
@Override
public void run() {
checkUpdates(false);
updateHandler.removeCallbacks(periodicUpdate); // remove whatever others may have posted
updateHandler.postDelayed(this, WAKEUP_INTERVAL);
}
};
private BroadcastReceiver connectivity_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
// do application-specific task(s) based on the current network state, such
// as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
boolean not_mobile = currentNetworkInfo.getTypeName().equalsIgnoreCase("MOBILE") ? false : true;
if( currentNetworkInfo.isConnected() && (mobile_updates || not_mobile) ) {
checkUpdates(false);
updateHandler.postDelayed(periodicUpdate, UPDATE_INTERVAL);
} else {
updateHandler.removeCallbacks(periodicUpdate); // no network anyway
}
}
};
private void setupVariables(Context ctx) {
context = ctx;
packageName = context.getPackageName();
preferences = context.getSharedPreferences( packageName + "_" + TAG, Context.MODE_PRIVATE);
device_id = crc32(Secure.getString( context.getContentResolver(), Secure.ANDROID_ID));
last_update = preferences.getLong("last_update", 0);
NOTIFICATION_ID += crc32(packageName);
// schedule.add(new ScheduleEntry(0,24));
ApplicationInfo appinfo = context.getApplicationInfo();
if( appinfo.icon != 0 ) {
appIcon = appinfo.icon;
} else {
Log_w(TAG, "unable to find application icon");
}
if( appinfo.labelRes != 0 ) {
appName = context.getString(appinfo.labelRes);
} else {
Log_w(TAG, "unable to find application label");
}
if( new File(appinfo.sourceDir).lastModified() > preferences.getLong(MD5_TIME, 0) ) {
preferences.edit().putString( MD5_KEY, MD5Hex(appinfo.sourceDir)).commit();
preferences.edit().putLong( MD5_TIME, System.currentTimeMillis()).commit();
String update_file = preferences.getString(UPDATE_FILE, "");
if( update_file.length() > 0 ) {
if( new File( context.getFilesDir().getAbsolutePath() + "/" + update_file ).delete() ) {
preferences.edit().remove(UPDATE_FILE).remove(SILENT_FAILED).commit();
}
}
}
raise_notification();
if( haveInternetPermissions() ) {
context.registerReceiver( connectivity_receiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
private boolean checkSchedule() {
if( schedule.size() == 0 ) return true; // empty schedule always fits
int now = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
for( ScheduleEntry e : schedule ) {
if( now >= e.start && now < e.end ) return true;
}
return false;
}
// required in order to prevent issues in earlier Android version.
private static void disableConnectionReuseIfNecessary() {
// see HttpURLConnection API doc
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
}
private static ArrayList<ScheduleEntry> schedule = new ArrayList<ScheduleEntry>();
private Runnable periodicUpdate = new Runnable() {
@Override
public void run() {
checkUpdates(false);
updateHandler.removeCallbacks(periodicUpdate); // remove whatever others may have posted
updateHandler.postDelayed(this, WAKEUP_INTERVAL);
}
};
private BroadcastReceiver connectivity_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
// do application-specific task(s) based on the current network state, such
// as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
boolean not_mobile = currentNetworkInfo.getTypeName().equalsIgnoreCase("MOBILE") ? false : true;
if( currentNetworkInfo.isConnected() && (mobile_updates || not_mobile) ) {
checkUpdates(false);
updateHandler.postDelayed(periodicUpdate, UPDATE_INTERVAL);
} else {
updateHandler.removeCallbacks(periodicUpdate); // no network anyway
}
}
};
private void setupVariables(Context ctx) {
context = ctx;
packageName = context.getPackageName();
preferences = context.getSharedPreferences( packageName + "_" + TAG, Context.MODE_PRIVATE);
device_id = crc32(Secure.getString( context.getContentResolver(), Secure.ANDROID_ID));
last_update = preferences.getLong("last_update", 0);
NOTIFICATION_ID += crc32(packageName);
// schedule.add(new ScheduleEntry(0,24));
ApplicationInfo appinfo = context.getApplicationInfo();
if( appinfo.icon != 0 ) {
appIcon = appinfo.icon;
} else {
Log_w(TAG, "unable to find application icon");
}
if( appinfo.labelRes != 0 ) {
appName = context.getString(appinfo.labelRes);
} else {
Log_w(TAG, "unable to find application label");
}
if( new File(appinfo.sourceDir).lastModified() > preferences.getLong(MD5_TIME, 0) ) {
preferences.edit().putString( MD5_KEY, MD5Hex(appinfo.sourceDir)).commit();
preferences.edit().putLong( MD5_TIME, System.currentTimeMillis()).commit();
String update_file = preferences.getString(UPDATE_FILE, "");
if( update_file.length() > 0 ) {
if( new File( context.getFilesDir().getAbsolutePath() + "/" + update_file ).delete() ) {
preferences.edit().remove(UPDATE_FILE).remove(SILENT_FAILED).commit();
}
}
}
raise_notification();
if( haveInternetPermissions() ) {
context.registerReceiver( connectivity_receiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
private boolean checkSchedule() {
if( schedule.size() == 0 ) return true; // empty schedule always fits
int now = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
for( ScheduleEntry e : schedule ) {
if( now >= e.start && now < e.end ) return true;
}
return false;
}
// required in order to prevent issues in earlier Android version.
private static void disableConnectionReuseIfNecessary() {
// see HttpURLConnection API doc
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
}