java - Android bugs on device. What should I do to fix them? -
in application noticed these 3 things:
-the button enabled when going 1 activity enabling user click on original activity. problem don't want user click on @ point in application. don't want disable button in application, when 1 intent called. how can that?
-i noticed strange... when toast notification pops in application until exit application. when exit application, of toast notifications residual , popping outside of application. there reason that? did miss in activity lifecycle handle cancellation of toasts @ point?
lastly, 1 rather tough solve. how lock screen when user rotates device, activity doesn't not called again , asynctask can still resume without starting on again?
thanks lot time. curious why these things happen , should into?
here's code:
//main activity.java package com.example.patient_device; import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.button; import java.io.*; public class mainactivity extends activity { //fields private progressdialog progressbar; private context context; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.start_setup); //set context context = this; //initialize start setup button , add onclick event listener button final button start_setup_button = (button) findviewbyid(r.id.start_setup_button); start_setup_button.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { //executes asynctask new retrieveinfotask().execute(); //instantiates intent launch new activity intent myintent = new intent(mainactivity.this, retrieveinfoactivity.class); mainactivity.this.startactivity(myintent); } }); } public class retrieveinfotask extends asynctask<void, void, void> { //called on ui thread execute progress bar @override protected void onpreexecute() { super.onpreexecute(); progressbar = new progressdialog(context); progressbar.setindeterminate(true); progressbar.setcancelable(false); progressbar.setmessage(mainactivity.this.getstring(r.string.retrieve_info)); progressbar.show(); } //methods retrieves information user device. performed in background thread private void retrieveinfo() { try { //reading drawable resource line line string str=""; stringbuffer buf = new stringbuffer(); inputstream = mainactivity.this.getresources().openrawresource(r.drawable.user_info); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); if (is!=null) { while ((str = reader.readline()) != null) { buf.append(str + "\n" ); } } is.close(); } catch (exception e) { e.printstacktrace(); } } //doinbackground calls retrieveinfo() perform action in background @override protected void doinbackground(void... params) { retrieveinfo(); return null; } //when background task done, dismiss progress bar @override protected void onpostexecute(void result) { if (progressbar!=null) { progressbar.dismiss(); } } } } //retrieveinfoactivity.java package com.example.patient_device; import android.app.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.content.res.resources; import android.os.batterymanager; import android.os.bundle; import android.util.log; import android.widget.textview; import android.widget.toast; public class retrieveinfoactivity extends activity { private static string tag = "retrieveinfoactivity"; private context context; string filelastsync = "09-18-2014 03:47 pm"; @override public void oncreate(bundle savedinstancestate) { context = this; super.oncreate(savedinstancestate); setcontentview(r.layout.retrieve_info); //once new activity launched, setup complete toast.maketext(getapplicationcontext(), "setup complete!", toast.length_long).show(); //gets 'last synced' string , sets datetime of last sync resources resources = context.getresources(); string syncstring = string.format(resources.getstring(r.string.last_sync), filelastsync); //dynamically sets datetime of last sync string textview lastsynctextview = ((textview) findviewbyid(r.id.last_sync) ); lastsynctextview.settext(syncstring); //calls registerreceiver receive broadcast state of battery this.registerreceiver(this.mbatinforeceiver,new intentfilter(intent.action_battery_changed)); } private broadcastreceiver mbatinforeceiver = new broadcastreceiver() { @override public void onreceive(context arg0, intent intent) { //battery level int level = intent.getintextra("level", 0); //dynamically sets value of battery level textview batterytextview = ((textview) findviewbyid(r.id.battery) ); batterytextview.settext("battery level: " + string.valueof(level)+ "%"); //if battery level drops below 25%, announce battery low //todo: add 25 constants file. if(level < 25) { toast.maketext(getapplicationcontext(), "low battery!", toast.length_long).show(); } //plugged in status int plugged = intent.getintextra(batterymanager.extra_plugged, -1); //battery status int status = intent.getintextra(batterymanager.extra_status, -1); //if device charging or contains full status, it's charging boolean ischarging = status == batterymanager.battery_status_charging || status == batterymanager.battery_status_full; //if device ischarging , plugged in, show battery charging if(ischarging && plugged == batterymanager.battery_plugged_ac || plugged == batterymanager.battery_plugged_usb) { toast.maketext(getapplicationcontext(), "charging.." + string.valueof(level)+ "%", toast.length_long).show(); }else{ toast.maketext(getapplicationcontext(), "unplugged!", toast.length_long).show(); } } }; @override public void ondestroy() { try { super.ondestroy(); unregisterreceiver(this.mbatinforeceiver); } catch (exception e) { log.e(retrieveinfoctivity.tag, getclass() + " releasing receivers-" + e.getmessage()); } } } //startsetupactivity.java import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; public class startsetupactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } } //fragmentsactivity.java import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class fragmentsactivity extends fragment{ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.main, container, false); } }
first of whenever want disable press override onbackpressed() method , remove super. this:
@override public void onbackpressed() { //super.onbackpressed(); }
second you'r using application context show toast. use activity context.
toast.maketext(this or youractivity.this, "setup complete!", toast.length_long).show();
third add attribute manifest class. avoid recrating activity when orientation change
android:configchanges="orientation"
Comments
Post a Comment