java - Android Application Not Responding Error -
i twice anr when swipe app recent activities. , app works fine anr when switch landscape mode. app works expected long not oriented landscape or swiped recent apps. can point out doing wrong here. thanks
myservice.java:
public class myservice extends service { public static boolean started = false; public static string transaction_done = "com.example.root.project"; private websocketclient client = null; public static sqlitedatabase mydatabase = null; @nullable @override public ibinder onbind(intent intent) { return null; } @override public int onstartcommand(final intent intent, int flags, int startid) { if(started){ string message = intent.getstringextra("message"); if(client != null){ client.send(message); } } if (!started) { mydatabase = openorcreatedatabase("messages.db", mode_private, null); mydatabase.execsql("create table if not exists messages " + "(tag text primary key, message text);"); started = true; uri uri = null; try { uri = new uri(intent.getstringextra("ip")); } catch (urisyntaxexception e) { e.printstacktrace(); } client = new websocketclient(uri) { @override public void onopen(serverhandshake serverhandshake) { jsonobject jsonobject = null; try { jsonobject = new jsonobject(); jsonobject.accumulate("addme",intent.getstringextra("name")); } catch (jsonexception e) { e.printstacktrace(); } client.send(jsonobject.tostring()); } @override public void onmessage(string message) { jsonobject jsonobject = null; try { jsonobject = new jsonobject(message); } catch (jsonexception e) { e.printstacktrace(); } if (jsonobject.has("resolve")) { pendingintent notificintent = pendingintent.getactivity(getapplicationcontext(), 0, new intent(getapplicationcontext(), mainactivity.class), 0); // builds notification notificationcompat.builder mbuilder = null; try { mbuilder = new notificationcompat.builder(getapplicationcontext()) .setsmallicon(r.drawable.add) .setcontenttitle(jsonobject.getstring("name")) .setticker(jsonobject.getstring("resolve")) .setcontenttext(jsonobject.getstring("destination")); } catch (jsonexception e) { e.printstacktrace(); } // defines intent fire when notification clicked mbuilder.setcontentintent(notificintent); // set default notification option // default_sound : make sound // default_vibrate : vibrate // default_lights : use default light notification mbuilder.setdefaults(notification.default_all); // auto cancels notification when clicked on in task bar mbuilder.setautocancel(true); // gets notificationmanager used notify user of background event notificationmanager mnotificationmanager = (notificationmanager) getapplicationcontext().getsystemservice(context.notification_service); // post notification mnotificationmanager.notify((int) system.currenttimemillis(), mbuilder.build()); } else if (jsonobject.has("remove")) { try { string tag = jsonobject.getstring("remove"); mydatabase.execsql("delete messages tag = " + tag + ";"); } catch (jsonexception e) { e.printstacktrace(); } if (isforeground(transaction_done)) { intent broadcast = new intent(transaction_done); broadcast.putextra("message", message); sendbroadcast(broadcast); } } else { try { mydatabase.execsql("insert messages (tag, message) values ('" + jsonobject.getstring("tag") + "', '" + message + "');"); } catch (jsonexception e) { e.printstacktrace(); } if (isforeground(transaction_done)) { intent broadcast = new intent(transaction_done); broadcast.putextra("message", message); sendbroadcast(broadcast); } } } @override public void onclose(int i, string s, boolean b) { log.d("mytag", "onclose: websocket closing "); toast.maketext(getapplicationcontext(),"closing websocket",toast.length_long).show(); } @override public void onerror(exception e) { log.i("websocket", "error " + e.getmessage()); } }; client.connect(); } return start_sticky; } @override public void ontaskremoved(intent rootintent) { super.ontaskremoved(rootintent); toast.maketext(getapplicationcontext(),"closing client",toast.length_long).show(); client.close(); } @override public void ondestroy() { super.ondestroy(); log.d("mytag", "ondestroy: service being destroyed "); try { client.closeblocking(); } catch (interruptedexception e) { e.printstacktrace(); } toast.maketext(this,"service closing on destroy called",toast.length_long).show(); } public boolean isforeground(string mypackage) { activitymanager manager = (activitymanager) getsystemservice(activity_service); list<activitymanager.runningtaskinfo> runningtaskinfo = manager.getrunningtasks(1); componentname componentinfo = runningtaskinfo.get(0).topactivity; return componentinfo.getpackagename().equals(mypackage); } }
mainactivity.java:
public class mainactivity extends appcompatactivity { private customadapter adapter; private string name; private string ip; @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.add) { final alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("title"); final view viewinflated = getlayoutinflater().inflate(r.layout.custom_dialog_layout, null); builder.setview(viewinflated); // set buttons builder.setpositivebutton("publish", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { edittext edittext = (edittext) viewinflated.findviewbyid(r.id.dialogtitle); edittext edittext2 = (edittext) viewinflated.findviewbyid(r.id.dialogdesc); string title = edittext.gettext().tostring().trim(); string desc = edittext2.gettext().tostring().trim(); jsonobject jsonobject = new jsonobject(); try { jsonobject.accumulate("title", title); jsonobject.accumulate("desc", desc); jsonobject.accumulate("name",name); } catch (jsonexception e) { e.printstacktrace(); } intent intent = new intent(mainactivity.this,myservice.class); intent.putextra("message",jsonobject.tostring()); startservice(intent); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show(); return true; } return super.onoptionsitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if(mypreferences.isfirst(this)) { final alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("title"); final view viewinflated = getlayoutinflater().inflate(r.layout.login_dialog, null); builder.setview(viewinflated); // set buttons builder.setpositivebutton("publish", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { edittext edittext = (edittext) viewinflated.findviewbyid(r.id.dialogname); edittext edittext2 = (edittext) viewinflated.findviewbyid(r.id.dialogip); name = edittext.gettext().tostring().trim(); ip = edittext2.gettext().tostring().trim(); intent intent = new intent(mainactivity.this, myservice.class); intent.putextra("name", name); intent.putextra("ip", ip); mainactivity.this.getsharedpreferences("my_preferences",mode_private).edit().putstring("name",name).putstring("ip",ip).commit(); startservice(intent); listview listview = (listview) findviewbyid(r.id.listview); adapter = new customadapter(mainactivity.this); listview.setadapter(adapter); intentfilter filter = new intentfilter(); filter.addaction(myservice.transaction_done); registerreceiver(myreceiver, filter); new handler().post(new runnable() { @override public void run() { if (myservice.mydatabase == null) return; cursor cursor = myservice.mydatabase.rawquery("select * messages", null); int messagecolumn = cursor.getcolumnindex("message"); cursor.movetofirst(); if (cursor != null && (cursor.getcount() > 0)) { { string message = cursor.getstring(messagecolumn); try { jsonobject jsonobject = new jsonobject(message); adapter.add(jsonobject.getstring("title"), jsonobject.getstring("desc"), jsonobject.getstring("tag")); } catch (jsonexception e) { e.printstacktrace(); } } while (cursor.movetonext()); adapter.update(); } } }); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show(); }else { intent intent = new intent(this, myservice.class); name = this.getsharedpreferences("my_preferences",mode_private).getstring("name","no_name"); ip = this.getsharedpreferences("my_preferences",mode_private).getstring("ip","no_ip"); intent.putextra("name",name); intent.putextra("ip",ip); startservice(intent); listview listview = (listview) findviewbyid(r.id.listview); adapter = new customadapter(this); listview.setadapter(adapter); intentfilter filter = new intentfilter(); filter.addaction(myservice.transaction_done); registerreceiver(myreceiver, filter); new handler().post(new runnable() { @override public void run() { if (myservice.mydatabase == null) return; cursor cursor = myservice.mydatabase.rawquery("select * messages", null); int messagecolumn = cursor.getcolumnindex("message"); cursor.movetofirst(); if (cursor != null && (cursor.getcount() > 0)) { { string message = cursor.getstring(messagecolumn); try { jsonobject jsonobject = new jsonobject(message); adapter.add(jsonobject.getstring("title"), jsonobject.getstring("desc"), jsonobject.getstring("tag")); } catch (jsonexception e) { e.printstacktrace(); } } while (cursor.movetonext()); adapter.update(); } } }); } } @override protected void ondestroy() { if (myreceiver != null) { unregisterreceiver(myreceiver); myreceiver = null; } super.ondestroy(); } private broadcastreceiver myreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string message = intent.getstringextra("message"); toast.maketext(mainactivity.this,message,toast.length_long).show(); jsonobject jsonobject = null; try { jsonobject = new jsonobject(message); if(jsonobject.has("remove")){ adapter.remove(jsonobject.getstring("remove")); adapter.update(); }else { adapter.add(jsonobject.getstring("title"), jsonobject.getstring("desc"), jsonobject.getstring("tag")); adapter.update(); } } catch (jsonexception e) { e.printstacktrace(); } } }; class customadapter extends baseadapter { private layoutinflater inflater; private arraylist<listviewdata> objects; public void add(string title, string desc, string tag) { objects.add(new listviewdata(title, desc, tag)); } public void remove(string tag){ for(listviewdata data : objects){ if(data.gettag().equals(tag)){ objects.remove(data); break; } } } public void update(){ notifydatasetchanged(); } private class viewholder { textview title; textview description; button resolve; button subscribe; button unsubscribe; } public customadapter(context context) { inflater = layoutinflater.from(context); objects = new arraylist<>(); } public int getcount() { return objects.size(); } public listviewdata getitem(int position) { return objects.get(position); } public long getitemid(int position) { return position; } public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.custom_list_layout, null); holder.title = (textview) convertview.findviewbyid(r.id.title); holder.description = (textview) convertview.findviewbyid(r.id.desc); holder.resolve = (button) convertview.findviewbyid(r.id.resolve); holder.subscribe = (button) convertview.findviewbyid(r.id.subscribe); holder.unsubscribe = (button) convertview.findviewbyid(r.id.unsubscribe); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.title.settext(objects.get(position).gettitle()); holder.description.settext(objects.get(position).getdescription()); holder.resolve.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { final alertdialog.builder builder = new alertdialog.builder(mainactivity.this); builder.settitle("title"); final view viewinflated = getlayoutinflater().inflate(r.layout.custom_dialog2, null); builder.setview(viewinflated); // set buttons builder.setpositivebutton("publish", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { edittext edittext = (edittext) viewinflated.findviewbyid(r.id.name); edittext edittext2 = (edittext) viewinflated.findviewbyid(r.id.destination); string name = edittext.gettext().tostring().trim(); string destination = edittext2.gettext().tostring().trim(); jsonobject jsonobject = new jsonobject(); try { jsonobject.accumulate("name", name); jsonobject.accumulate("destination", destination); jsonobject.accumulate("resolve",objects.get(position).gettag()); } catch (jsonexception e) { e.printstacktrace(); } intent intent = new intent(mainactivity.this,myservice.class); intent.putextra("message",jsonobject.tostring()); startservice(intent); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show(); } }); holder.subscribe.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string tag = objects.get(position).gettag(); try { intent intent = new intent(mainactivity.this,myservice.class); intent.putextra("message",new jsonobject().accumulate("subscribe",tag).accumulate("name",name).tostring()); startservice(intent); } catch (jsonexception e) { e.printstacktrace(); } } }); holder.unsubscribe.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string tag = objects.get(position).gettag(); try { intent intent = new intent(mainactivity.this,myservice.class); intent.putextra("message",new jsonobject().accumulate("unsubscribe",tag).accumulate("name",name).tostring()); startservice(intent); } catch (jsonexception e) { e.printstacktrace(); } } }); return convertview; } } } class mypreferences { private static final string my_preferences = "my_preferences"; public static boolean isfirst(context context){ final sharedpreferences reader = context.getsharedpreferences(my_preferences, context.mode_private); final boolean first = reader.getboolean("is_first", true); if(first){ final sharedpreferences.editor editor = reader.edit(); editor.putboolean("is_first", false); editor.commit(); } return first; } }
Comments
Post a Comment