How to auto update a continuously running Android app without user interaction -
we have app in google play store runs in foreground continuously. devices runs on out of our control , not rooted. run on either android 4.2 or 4.4.
our goal have app update newest version release via play store without user interaction. restarting device acceptable "interaction" option.
we find running app not updated automatically when running if "automatic update" turned on.
what way achieve our goal?
use alarm manager scheduled update , use create class , extend service or intentservice class. check if theres internet connection if yes proceed update this: check link android services - tutorial in way can update not showing activity using service.
creating alarm manager:
calendar cal = calendar.getinstance(); intent intent = new intent(this, myservice.class); pendingintent pintent = pendingintent.getservice(this, 0, intent, 0); alarmmanager alarm = (alarmmanager)getsystemservice(context.alarm_service); // start every 30 seconds alarm.setrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), 30*1000, pintent);
for service:
public class downloadservice extends intentservice { private int result = activity.result_canceled; public static final string url = "urlpath"; public static final string filename = "filename"; public static final string filepath = "filepath"; public static final string result = "result"; public static final string notification = "com.vogella.android.service.receiver"; public downloadservice() { super("downloadservice"); } // called asynchronously android @override protected void onhandleintent(intent intent) { string urlpath = intent.getstringextra(url); string filename = intent.getstringextra(filename); file output = new file(environment.getexternalstoragedirectory(), filename); if (output.exists()) { output.delete(); } inputstream stream = null; fileoutputstream fos = null; try { url url = new url(urlpath); stream = url.openconnection().getinputstream(); inputstreamreader reader = new inputstreamreader(stream); fos = new fileoutputstream(output.getpath()); int next = -1; while ((next = reader.read()) != -1) { fos.write(next); } // finished result = activity.result_ok; } catch (exception e) { e.printstacktrace(); } { if (stream != null) { try { stream.close(); } catch (ioexception e) { e.printstacktrace(); } } if (fos != null) { try { fos.close(); } catch (ioexception e) { e.printstacktrace(); } } } publishresults(output.getabsolutepath(), result); } private void publishresults(string outputpath, int result) { intent intent = new intent(notification); intent.putextra(filepath, outputpath); intent.putextra(result, result); sendbroadcast(intent); } }
Comments
Post a Comment