hi all
i am trying service to day, my task is to run my sample app in background and to be able to stor the data in the database but every minute.
for this task i have created an activity called gps tracking
and the code is the following
the php code
the code for service
all the code is tested and working, the only part that i need is an example of the service,
i understant how the service is runing but i am missing what i must do.
my target in this example is to store the gps data for example every minute and to be able to have the app working in background.
an example, how the service code must be is most apricated so i can go over witth the debager to understand how the program flows.
thanks in advance
i am trying service to day, my task is to run my sample app in background and to be able to stor the data in the database but every minute.
for this task i have created an activity called gps tracking
and the code is the following
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Dim GPS1 As GPS
Dim currentURL As String
End Sub
Sub Globals
Dim lblLon As Label
Dim lblLat As Label
Dim lblSpeed As Label
Dim lblSatellites As Label
Dim mybatlevel As Int
Dim mylat As Double
Dim mylong As Double
Dim myspeed As Int
Dim mydate As String
Dim mytime As String
Dim myusername As String
Private btnLogout As Button
Private lblMessage As Label
Private bat_lvl As Label
Dim hg As PhoneEvents
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
GPS1.Initialize("GPS")
End If
Activity.LoadLayout("frmtracking")
lblMessage.Text = "Welcome, " & Login.strUserName
ToastMessageShow(currentURL, False)
'for battery
hg.Initialize("hg")
End Sub
Sub hg_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
bat_lvl.Text = "Battery = " & Level & "%"
mybatlevel=Level
End Sub
Sub Activity_Resume
If GPS1.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
GPS1.Start(0, 0) 'Listen to GPS with no filters.
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
GPS1.Stop
End Sub
Sub GPS_LocationChanged (Location1 As Location)
DateTime.dateformat = "yyyy/MM/dd"
DateTime.TimeFormat= "HH:MM:ss"
mylat=Location1.Latitude
mylong=Location1.Longitude
myusername= Login.strUserName
lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
lblSpeed.Text = "Speed = " & (Location1.Speed * 3.6)
myspeed= Location1.Speed * 3.6
mydate=DateTime.date(DateTime.now)
mytime=DateTime.Time(DateTime.Now)
Dim InsertNewPossition As HttpJob
InsertNewPossition.Initialize("InsertNewPossition", Me)
InsertNewPossition.download2("http://www.mydomainname.com/gpsstore.php", Array As String ("action", "InsertNewPossition", "user_name", myusername, "date", mydate, "time", mytime,"latitude", mylat, "longitude", mylong,"speed", myspeed ,"battery", mybatlevel))
Msgbox("saved", myusername)
End Sub
Sub GPS_UserEnabled (Enabled As Boolean)
ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
Sub GPS_GpsStatus (Satellites As List)
lblSatellites.Text = "Satellites:" & CRLF
For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite
Satellite = Satellites.Get(i)
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
& " " & Satellite.Elevation
Next
End Sub
Sub JobDone(Job As HttpJob)
ProgressDialogHide
If Job.Success Then
Dim res As String
res = Job.GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select Job.JobName
Case "InsertNewPossition"
'Do nothing
End Select
Else
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
B4X:
<?
$host = "localhost";
$db = "db";
$user = "dbuser";
$pw = "123";
//include ("db.php");
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
$action = $_GET["action"];
switch ($action)
{
case "CountPosistions":
$q = mysql_query("SELECT * FROM tbl_track");
$count = mysql_num_rows($q);
print json_encode($count);
break;
Case "GetPossition":
$q = mysql_query("SELECT * FROM tbl_track");
$rows = array();
while($r = mysql_fetch_assoc($q))
{
$rows[] = $r;
}
print json_encode($rows);
break;
case "InsertNewPossition":
$user_name= $_GET["user_name"];
$date= $_GET["date"];
$time= $_GET["time"];
$latitude= $_GET["latitude"];
$longitude= $_GET["longitude"];
$speed= $_GET["speed"];
$battery= $_GET["battery"];
$q = mysql_query("INSERT INTO tbl_track (user_name, date, time, latitude, longitude, speed, battery) VALUES ('$user_name', '$date', '$time', '$latitude', '$longitude', '$speed', '$battery' )");
print json_encode("Inserted");
break;
}
?>
the code for service
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim noti As Notification
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
noti.Initialize
noti.Light = True
noti.Vibrate = True
noti.OnGoingEvent = False
noti.Sound = True
noti.Icon = "icon"
noti.SetInfo("Reminder","Something to do today!")
noti.AutoCancel=True
noti.Notify(1)
ToastMessageShow("Please read the remminder and delete it from list!", False)
StopService(Me)
CancelScheduledService(Me)
End Sub
Sub Service_Destroy
StopService("") 'Stop this service
CancelScheduledService("") 'Cancel this service
End Sub
i understant how the service is runing but i am missing what i must do.
my target in this example is to store the gps data for example every minute and to be able to have the app working in background.
an example, how the service code must be is most apricated so i can go over witth the debager to understand how the program flows.
thanks in advance