android - how should i get my gps detection get going..? -
i have created togglebutton on checking want myservices class start service... main layout file has 3 buttons , on clicking 2nd button userstatus activity started , toggle on activity has startservice() think starts myservice toasts gave in in between displayed.now intend geolocation detection in background don't know why geolocation not happening,i did using video youtube
this userstatus activity
package com.salesforce.samples.templateapp; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.compoundbutton; import android.widget.switch; import android.widget.toast; import android.widget.togglebutton; import android.location.*; import android.content.intent; public class userstatus extends activity { togglebutton statustoggle; @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_user_status, menu); return true; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_status); statustoggle=(togglebutton)findviewbyid(r.id.togglebutton1); statustoggle.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if (ischecked) { statustoggle.settexton("on"); toast.maketext(getapplicationcontext(), "toggle on", toast.length_long).show(); startservice(new intent(getbasecontext(), myservices.class)); } else { statustoggle.settextoff("off"); toast.maketext(getapplicationcontext(), "toggle off", toast.length_long).show(); stopservice(new intent(getbasecontext(), myservices.class)); } } }); } }
this myservices class
package com.salesforce.samples.templateapp; import java.util.hashmap; import org.json.jsonarray; import com.salesforce.androidsdk.app.forceapp; import com.salesforce.androidsdk.rest.restclient; import com.salesforce.androidsdk.rest.restrequest; import com.salesforce.androidsdk.rest.restresponse; import com.salesforce.androidsdk.rest.restclient.asyncrequestcallback; import android.app.alertdialog; import android.app.dialog; import android.app.dialogfragment; import android.app.service; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.provider.settings; import android.util.log; import android.widget.textview; import android.widget.toast; public class myservices extends service { restclient client; double plat; double plong; int two_min=5*60*1000; // textview infotext; public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(this, "location updation has started", toast.length_long) .show(); detectlocation(); return start_sticky; } private void detectlocation() { // todo auto-generated method stub toast.maketext(this, "inside detectlocation()", toast.length_short) .show(); locationmanager lm1 = (locationmanager) getsystemservice(context.location_service); locationlistener ll1 = new mylocationlistetner(); log.v("x", "response:after creating lm , ll "); lm1.requestlocationupdates(locationmanager.gps_provider, 10, two_min, ll1); log.v("x", "response:after lm1.requestlocationupdates "); ll1.onlocationchanged(null); } class mylocationlistetner implements locationlistener { @override public void onlocationchanged(location location) { // todo auto-generated method stub log.v("x","response:inside onlocationchanged "); log.v("x","response:"+location); if (location == null) { plat = location.getlatitude(); plong = location.getlongitude(); log.v("x", "response:location " + double.tostring(plat)+double.tostring(plong)); string objecttype = "akshayg__user__c"; string objectid = "a02900000089fk3"; hashmap<string, object> fields = new hashmap<string, object>(); fields.put("name", "ashish"); // fields.put("akshayg__donor_location__latitude__s", // double.tostring(plat)); // fields.put("akshayg__donor_location__longitude__s", // double.tostring(plong)); restrequest request = null; try { request = restrequest.getrequestforupdate( getstring(r.string.api_version), objecttype, objectid, fields); // toast.maketext(this, "location updation has started", // toast.length_long).show(); } catch (exception e) { // printheader("could not build update request"); printexception(e); return; } client.sendasync(request, new asyncrequestcallback() { @override public void onsuccess(restrequest request, restresponse result) { // toast.maketext(this, // ""+double.tostring(plat)+","+double.tostring(plong), // toast.length_long).show(); try { //toast.maketext(this, "location updated",toast.length_long).show(); log.v("x", "response: " + result.tostring()); /*jsonarray records = result.asjsonobject() .getjsonarray("records"); (int = 0; < records.length(); i++) { // listadapter.add(records.getjsonobject(i).getstring("name")); // listadapter.add(records.getjsonobject(i).getstring("akshayg__phone_number__c")); // listadapter.add(records.getjsonobject(i).getstring("akshayg__donor_location__latitude__s")); // listadapter.add(records.getjsonobject(i).getstring("akshayg__donor_location__longitude__s")); }*/ } catch (exception e) { onerror(e); } } @override public void onerror(exception exception) { log.v("x", "response: " + exception.tostring()); // toast.maketext(mainactivity.this, // mainactivity.this.getstring(forceapp.app.getsalesforcer().stringgenericerror(), // exception.tostring()), // toast.length_long).show(); } }); } } private void printexception(exception e) { string err = "error: " + e.getclass().getsimplename(); toast.maketext(getapplicationcontext(), err, toast.length_long) .show(); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } } public void ondestroy() { super.ondestroy(); toast.maketext(this, "location updation has stoped", toast.length_long) .show(); } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } }
here log cat errors m getting..
04-02 23:01:33.375: e/androidruntime(4779): fatal exception: main 04-02 23:01:33.375: e/androidruntime(4779): java.lang.runtimeexception: unable start service com.salesforce.samples.templateapp.myservices@413c8878 intent { cmp=com.salesforce.samples.templateapp/.myservices }: android.util.androidruntimeexception: calling startactivity() outside of activity context requires flag_activity_new_task flag. want? 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.activitythread.handleserviceargs(activitythread.java:2403) 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.activitythread.access$1900(activitythread.java:127) 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.activitythread$h.handlemessage(activitythread.java:1221) 04-02 23:01:33.375: e/androidruntime(4779): @ android.os.handler.dispatchmessage(handler.java:99) 04-02 23:01:33.375: e/androidruntime(4779): @ android.os.looper.loop(looper.java:137) 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.activitythread.main(activitythread.java:4448) 04-02 23:01:33.375: e/androidruntime(4779): @ java.lang.reflect.method.invokenative(native method) 04-02 23:01:33.375: e/androidruntime(4779): @ java.lang.reflect.method.invoke(method.java:511) 04-02 23:01:33.375: e/androidruntime(4779): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:823) 04-02 23:01:33.375: e/androidruntime(4779): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:590) 04-02 23:01:33.375: e/androidruntime(4779): @ dalvik.system.nativestart.main(native method) 04-02 23:01:33.375: e/androidruntime(4779): caused by: android.util.androidruntimeexception: calling startactivity() outside of activity context requires flag_activity_new_task flag. want? 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.contextimpl.startactivity(contextimpl.java:877) 04-02 23:01:33.375: e/androidruntime(4779): @ android.content.contextwrapper.startactivity(contextwrapper.java:276) 04-02 23:01:33.375: e/androidruntime(4779): @ com.salesforce.samples.templateapp.myservices.onstartcommand(myservices.java:43) 04-02 23:01:33.375: e/androidruntime(4779): @ android.app.activitythread.handleserviceargs(activitythread.java:2386)
the control going till after onlocationchanged() not entering if...else condition. bcoz m sending null service class onlocationchanged()..? if check location!=null in if condition control still doesn't go pass if statement.. please give suggest corrections...!!
as abhijit said, easier logcat error, because error gave 1 displayed android, exception raised in code.
if had guess, maybe userstatus
activity not declared in manifest.
edit: way, make sure gps enabled on device test on. might want verify programatically in way on android developers website, , open location settings if not enabled. check out here.
Comments
Post a Comment