Notify user about location updates automatically android -
i'm trying android hive's tutorial understand how track location of user , show them places nearby. want notify user location change , redirect them first screen of app. have tried calling class contains notification code onlocationchanged method, shows null pointer exception @ pendingintent.getactivity().i have added permissions in manifest file here's code:
gpstracker.java:
this file contain onlocationchanged i'm trying call shownotif(). public class gpstracker extends service implements locationlistener {
private final context mcontext; notif n = new notif(); // flag gps status boolean isgpsenabled = false; // flag network status boolean isnetworkenabled = false; // flag gps status boolean cangetlocation = false; location location = null; // location double latitude; // latitude double longitude; // longitude // minimum distance change updates in meters private static final long min_distance_change_for_updates = 0; // minimum time between updates in milliseconds private static final long min_time_bw_updates = 0; // declaring location manager protected locationmanager locationmanager; public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) mcontext .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } toast.maketext(getapplicationcontext(), "location changed",toast.length_short).show(); } catch (exception e) { e.printstacktrace(); } return location; } /** * stop using gps listener calling function stop using gps in * app * */ public void stopusinggps() { if (locationmanager != null) { locationmanager.removeupdates(gpstracker.this); } } /** * function latitude * */ public double getlatitude() { if (location != null) { latitude = location.getlatitude(); } // return latitude return latitude; } /** * function longitude * */ public double getlongitude() { if (location != null) { longitude = location.getlongitude(); } // return longitude return longitude; } /** * function check gps/wifi enabled * * @return boolean * */ public boolean cangetlocation() { return this.cangetlocation; } /** * function show settings alert dialog on pressing settings button * lauch settings options * */ public void showsettingsalert() { alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog .setmessage("gps not enabled. want go settings menu?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent intent = new intent( settings.action_location_source_settings); mcontext.startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); } @override public void onlocationchanged(location location) { //notification if(location!=null) { n.shownotif(this); } } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public ibinder onbind(intent arg0) { return null; }
}
this notif.java
public class notif { void shownotif(context context) { pendingintent pi = pendingintent.getactivity(context, 0, new intent(context, mainactivity.class), 0); notification noti = new notificationcompat.builder(context) .setcontenttitle("notification") .setcontenttext("there new places visit") .setsmallicon(android.r.drawable.stat_sys_warning) .setcontentintent(pi) .setdefaults(notification.default_sound) .setdefaults(notification.default_vibrate) .setwhen(system.currenttimemillis()) .setautocancel(true) .build(); notificationmanager mgr = (notificationmanager)context.getsystemservice(context.notification_service); mgr.notify(0,noti); }
}
and mainactivity.java oncreate method
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); cd = new connectiondetector(getapplicationcontext()); // check if internet present isinternetpresent = cd.isconnectingtointernet(); if (!isinternetpresent) { // internet connection not present alert.showalertdialog(mainactivity.this, "internet connection error", "please connect working internet connection", false); // stop executing code return return; } // creating gps class object gps = new gpstracker(this); // check if gps location can if (gps.cangetlocation()) { log.d("your location", "latitude:" + gps.getlatitude() + ", longitude: " + gps.getlongitude()); } else { // can't user's current location alert.showalertdialog(mainactivity.this, "gps status", "couldn't location information. please enable gps", false); // stop executing code return return; } // getting listview lv = (listview) findviewbyid(r.id.list); // button show on map btnshowonmap = (button) findviewbyid(r.id.btn_show_map); // calling background async task load google places // after getting places google data shown in listview new loadplaces().execute(); /** button click event shown on map */ btnshowonmap.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent = new intent(getapplicationcontext(), placesmapactivity.class); // sending user current geo location i.putextra("user_latitude", double.tostring(gps.getlatitude())); i.putextra("user_longitude", double.tostring(gps.getlongitude())); // passing near places map activity i.putextra("near_places", nearplaces); // staring activity startactivity(i); } }); /** * listitem click event * on selecting listitem singleplaceactivity launched * */ lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string reference = ((textview) view.findviewbyid(r.id.reference)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singleplaceactivity.class); // sending place refrence id single place activity // place refrence id used "place full details" in.putextra(key_reference, reference); startactivity(in); } }); }
i have tried calling class contains notification code onlocationchanged method, shows null pointer exception @ pendingintent.getactivity().
pendingintent pi = pendingintent.getactivity(context, 0, new intent(context, mainactivity.class), 0);
as getting getting null in context , pass mcontext inside onlocationchanged .e.g
@override public void onlocationchanged(location location) { //notification if(location!=null) { n.shownotif(mcontext); }
Comments
Post a Comment