cordova - Android Not Responding throws in Service -


i'm developing app should track user location time time (e.g. every 20 seconds) , @ each minute send locations web services. achieve that, i've created producer consumer structure each task android service started alarmmanagers/broadcastreceivers. application has ui developed phonegap , jquery mobile secondary tasks , send data services(e.g. username). problem occurs when service responsible network communication stuck getinputstream() method , throws android not responding dialog. understand lengthy operations should not performed ui thread, i'm not sure how solve problem given approach, clue?

below app manifest , code:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           package="com.android.fcl"           android:versioncode="1"               android:versionname="1.0" >  <uses-sdk     android:minsdkversion="8"     android:targetsdkversion="17" />  <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location"   /> <uses-permission android:name="android.permission.access_location_extra_commands" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.battery_stats" /> <uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.flashlight" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.wake_lock" />  <application     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme"     android:configchanges="orientation|keyboardhidden"      android:allowbackup="false">     <activity         android:name="com.android.fcl.mainactivity"         android:label="@string/app_name"          android:screenorientation="portrait">         <intent-filter>             <action android:name="android.intent.action.main" />             <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <service          android:name="com.android.fcl.gpsloggerservice"         android:label="@string/gps_logger_service">     </service>     <service          android:name="com.android.fcl.gpshttpservice"         android:label="@string/gps_http_service">     </service>     <receiver android:name="com.android.fcl.gpsloggerreceiver" />     <receiver android:name="com.android.fcl.gpshttpreceiver" /> </application>  </manifest> 

javascript:

function gotopage() {     window.servicecontrol.startservice();     $.mobile.changepage('address.html', {transition: 'flip'});     $.mobile.hidepageloadingmsg(); } 

activity:

public class mainactivity extends droidgap {      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         super.init();          if (android.os.build.version.sdk_int > 9) {             strictmode.threadpolicy policy = new    strictmode.threadpolicy.builder().permitall().build();             strictmode.setthreadpolicy(policy);         }          servicecontrol service = servicecontrol.getinstance();         service.setactivity(this);         service.setview(appview);         appview.addjavascriptinterface(service, "servicecontrol");          servicestorage servicestorage = servicestorage.getinstance();         servicestorage.createdatabase(this);         super.loadurl("file:///android_asset/www/login.html");     }     } 

servicecontrol.java:

public class servicecontrol {      private alarmmanager gpsalarmmanager;     private pendingintent gpspendingintent;     private alarmmanager httpalarmmanager;     private pendingintent httppendingintent;      public void startservice() {         if (this.activity != null) {             intent gpsintent = new intent(this.activity, gpsloggerreceiver.class);             intent httpintent = new intent(this.activity, gpshttpreceiver.class);             if (this.gpsalarmmanager == null) {                 gpsalarmmanager = (alarmmanager) activity.getsystemservice(service.alarm_service);                 gpspendingintent = pendingintent.getbroadcast(activity, 0, gpsintent, 0);                 gpsalarmmanager.setrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime(), gps_min_time, gpspendingintent);             }             if (this.httpalarmmanager == null) {                 httpalarmmanager = (alarmmanager) activity.getsystemservice(service.alarm_service);                 httppendingintent = pendingintent.getbroadcast(activity, 0, httpintent, 0);                 httpalarmmanager.setrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime(), http_min_time, httppendingintent);             }     } } 

broadcastreceiver:

public class gpshttpreceiver extends broadcastreceiver {      @override     public void onreceive(context context, intent intent) {         intent service = new intent(context, gpshttpservice.class);         context.startservice(service);     } } 

service:

public class gpshttpservice extends service {      private powermanager pm = null;     private powermanager.wakelock wl = null;      private httpurlconnection httpconnection = null;      private synchronized void setwakelock() {         servicecontrol servicecontrol = servicecontrol.getinstance();         droidgap activity = servicecontrol.getactivity();         pm = (powermanager) activity.getsystemservice(context.power_service);         wl = pm.newwakelock(powermanager.partial_wake_lock, tag);         wl.acquire();     }      private string sendpostmessage(string message) {         byte [] buffer = null;         string response = null;     url url = null;         dataoutputstream outputstream = null;         inputstreamreader inputstream = null;          buffer = message.getbytes();         try {             url = new url(uri);             httpconnection = (httpurlconnection) url.openconnection();             httpconnection.setdooutput(true);             httpconnection.setdoinput(true);             httpconnection.setinstancefollowredirects(false);             httpconnection.setrequestmethod(fclstring.post_request_method);             httpconnection.setrequestproperty(fclstring.content_type_property, fclstring.content_type_app_json);             httpconnection.setrequestproperty(fclstring.charset_property, fclstring.utf8_format);             httpconnection.setconnecttimeout(conn_timeout);             httpconnection.setrequestproperty(fclstring.content_length_property, string.valueof(buffer.length));             httpconnection.setusecaches(false);             outputstream = new dataoutputstream(httpconnection.getoutputstream());             outputstream.write(buffer);             outputstream.flush();             outputstream.close();             inputstream = new inputstreamreader(httpconnection.getinputstream());             response = gpsmessage.readresponsestream(inputstream);        } catch (malformedurlexception e) {            log.d(tag, fclstring.malformed_url);        } catch (ioexception e) {            log.d(tag, fclstring.io_exception);            log.d(tag,  e.getmessage());        } {            if (httpconnection != null) {                httpconnection.disconnect();             }        }        return response;     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         servicestorage servicestorage = servicestorage.getinstance();         jsonobject json= null;         string message = null;         string response = null;         boolean status = false;          super.onstartcommand(intent, flags, startid);     setwakelock();         json = servicestorage.getrecords();         if ((json != null) && (json.length() > 0)) {             message = gpsmessage.preparemessage(json);             if (message != null) {                 if (hasinternetconnection()) {                     response = sendpostmessage(message);                     status = gpsmessage.evaluateresponse(response);                     if (status) {                         servicestorage.removerecords(json);                     }                 }              }         }         this.stopself();         return service.start_not_sticky;      } } 

from reference doc on android services:

note services, other application objects, run in main thread of hosting process. means that, if service going cpu intensive (such mp3 playback) or blocking (such networking) operations, should spawn own thread in work. more information on can found in processes , threads. intentservice class available standard implementation of service has own thread schedules work done.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -