Android Question StartService doesn't work

cncncn

Member
Licensed User
Hello

My servicestart doesn't work and I dont know why its not working.
here is my code:
B4X:
Version=2.71
IconFile=
NumberOfModules=3
Module1=widget
ModuleVisible1=1
Module2=bibli
ModuleVisible2=1
Module3=SMBService
ModuleVisible3=1
Package=b4a.example
DoNotOverwriteManifest=True
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>~\n~<supports-screens android:largeScreens="true" ~\n~    android:normalScreens="true" ~\n~    android:smallScreens="true" ~\n~    android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~'End of default text.~\n~AddActivityText(Main,<intent-filter>~\n~<action android:name="com.maximussoft.myprovider.REQUEST"/>~\n~<category android:name="android.intent.category.DEFAULT"/>~\n~</intent.filter>)
UserTypesHint=
NumberOfFiles=1
File1=main.bal
NumberOfLibraries=2
Library1=core
Library2=smb
@EndOfDesignText@
#Region  Project Attributes
    #ApplicationLabel: Modification
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'Version 1.0
'dernière mise à jour faite le 28.04.2014
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim fichier As Int '0=Plan de Q.F. 1=DT 2=N
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
   
    Activity.LoadLayout("main")
    Activity.SendToBack
'    fichier=1
'    If fichier =1 Then
'   
'        SMBService.Src        = "smb://192.168.1.60/GPAONGV/DOSSIERS/NGTS2222/CLANGDT012/"
'        SMBService.Fil        = "jeton.txt"
'        SMBService.Action    = 400
'       
'       
'    End If
'
'    StartService(SMBService)
'    If SMBService.DoneSuccessfully Then
'        ToastMessageShow("modification impossible",True)
'           
'    Else
'        ToastMessageShow("modification possible",True)
'    End If
    Dim dest As String
    dest=File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt")
   
   
            SMBService.Src        = File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt")
            SMBService.Fil        = "jeton.txt"
            SMBService.Action    = 400
            StartService(SMBService)
            If SMBService.DoneSuccessfully Then
                ToastMessageShow("modif pas possible",True)
            Else
                ToastMessageShow("modif possible",True)
            End If   
    Activity.Finish
   

End Sub

Sub Activity_Resume
'    Dim In As Intent
'    In=Activity.GetStartingIntent
'    If In <> Null Then
'        Log("envoie 1")
'        If In.HasExtra("GetSettings") Then
'            SendSettings(In)
'            Log("envoie 2")
'            Return
'        End If
'    End If   
'   
'    Activity.Finish
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
'fonction delay
Sub Delay(nMilliSecond As Long)As Boolean
Dim nBeginTime As Long
Dim nEndTime As Long
nEndTime = DateTime.Now + nMilliSecond
nBeginTime = DateTime.Now
Do While nBeginTime < nEndTime
nBeginTime = DateTime.Now
'Log(nBeginTime)
If nEndTime < nBeginTime Then
Return True
End If
DoEvents
Loop
End Sub

Sub SendSettings(In As Intent)
    Dim returnIntent As Intent
    returnIntent.Initialize(In.GetExtra("CallBack"),"")
    returnIntent.AddCategory("android.intent.category.DEFAULT")
    returnIntent.PutExtra("Settings","true")
    returnIntent.PutExtra("Successful","true")
    StartService(returnIntent)

End Sub
With my SMBService :
B4X:
Type=Service
Version=2.71
@EndOfDesignText@
'Module Service : SMB
#Region Module Attributes
    #StartAtBoot: False
#End Region
Sub Process_Globals
   
    'Src signifie Serveur et Dest = Tablette
   
   
   
   
    Dim usismb As SMB
    'Activity is expected to set URL
    Dim Src,Dest,Fil,Fil2,Jour, Heure As String
    Dim Listfil As List
   
    Dim Action As Int
   
   
    Dim DoneSuccessfully As Boolean
    Dim Notification1 As Notification
    Dim Abandon As Boolean
End Sub
Sub Service_Create
    'Initialisation du Module SMBservrice
    usismb.Initialize("usismb")
    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = True
    Log ("SMB : Début du service")
End Sub

    'Récapitulatif des actions possibles
    'Action = 100 : Copie d'un fichier du serveur à la tablette
    'Action = 200 : Copie d'un fichier de la tablette au serveur
    'Action = 300 : Comparaison de la date de modification
    'Action = 400 : Recherche liste fichiers
    'Action = 500 : Supression d'un fichier ou dossier
    '______________________________________________________________________
Sub Service_Start
    'Initialisation de la connexion au serveur
    usismb.SetCredentials(bibli.UtilisateurServeur,bibli.MotDePasseServeur,bibli.DomaineServeur)
    DoneSuccessfully= False
    Abandon = False
    If Action = 4000 Then 'copie du fichier texte nommé jeton vers le repertoire
    Log("copie du jeton")
        Log(Dest)
        Log(Fil)
        Log(Src)
        usismb.UploadFile(Dest,Fil,Src,Fil2)'Copie du fichier
    End If   
    If Action = 202 Then' sauvegarde Qualité-Fabrication
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier
        'bibli.valeur=4011
        ToastMessageShow("Plan Qualité-Fabrication sauvegardé sur le serveur",False)   
    End If
    If Action = 201 Then ' sauvegarde page notation
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier
        ToastMessageShow("page sauvegardé sur le serveur",False)   
    End If
    If Action = 203 Then ' sauvegarde photo
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier   
    End If
    If Action = 100 Then
        usismb.DownloadFile(Src,Fil,Dest,Fil)'Copie du fichier
       
    'Copier un fichier de la tablette au serveur
    Else If Action = 200 Then
        'If bibli.TesteVerrouDT()=False Then
        '    usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier
        '    bibli.valeur=4001
        '    CallSub(Main,"VerrouilleDT")
        '    CallSub(Main,"AfficheProtege")
    '    Else
            'bibli.Msgbox1("Dossier technique verrouillé en écriture."&CRLF&"Aucune modification n'a été prise en compte.","Information")
        '    CallSub(Main,"MainMsgBox1")
        '    Abandon = True
        'End If
    'Liste des fichiers
    Else If(Action = 300 OR Action = 400) Then
        Log("ttt")
        usismb.ListFiles(Src,Fil)'Recherche les fichiers
   
    Else If Action = 500 Then
        usismb.DeleteFile(Src,Fil)'Suppresion d'un dossier ou fichier
    End If
    If Abandon = False Then
       
        'Affichage d une notification dans la barre des tâches
        Notification1.SetInfo("SMB service en cours", "En cours: " & Fil, Main)
        Notification1.Sound = False'Pas du son
        Service.StartForeground(1, Notification1)   
    Else
        Fin
    End If
   
   
End Sub
'Signale la fin de la copie du serveur à la tablette
Sub usismb_DownloadCompleted (Url As String, RemoteFile As String, Success As Boolean)
    If Not(Success) Then
        Log(LastException)
        DoneSuccessfully = False
        Fin
    Else
        'ToastMessageShow( "Download Completed", True)
        Log("SMB : Copie terminée")
       
        DoneSuccessfully = Success
        Fin
    End If
   
   
   
End Sub
'Signale la fin de la copie de la tablette au serveur
Sub usismb_UploadCompleted (Url As String, RemoteFile As String, Success As Boolean)
    If Not(Success) Then
        Log(LastException)
    Else
        'ToastMessageShow( "Download Completed", True)
        Log("SMB : Copie terminée")
        DoneSuccessfully = Success
    End If
    Fin
End Sub


'Récupère la date de modification
Sub usismb_ListCompleted(Url As String, Success As Boolean, Entries() As SMBFile)
    If Not(Success) Then
        Log(LastException)
    Else
        Listfil.Initialize
        Log("SMB : Listage fichiers")
        For i = 0 To Entries.Length - 1
           
            If Action= 300 Then
               
                Jour = DateTime.Date(Entries(i).LastModified)
                Heure = DateTime.Time(Entries(i).LastModified)
                Log(Heure)
                Log(Jour)
               
                taille = Entries(i).Size
            Else If  Action = 400 Then
                Delay(200)
                Listfil.Add(Entries(i).Name)
                Delay(200)
            End If
        Next
        DoneSuccessfully = Success
       
    End If
    Fin
End Sub
'Signale la fin de la supression du fichier
Sub usismb_DeleteCompleted (Url As String, RemoteFile As String, Success As Boolean)
    If Not(Success) Then
        Log(LastException)
    Else
        DoneSuccessfully = Success
        Log("SMB : Suppression terminée")
    End If
    Fin
End Sub
Sub Fin
    Log("SMB : Fin du service")
   
   
    Service.StopForeground(1) 'Retourne le service en 'background' (supprime aussi la notification)
    'Si l'activité est en pause, une notification est envoyée
    If IsPaused(Main) Then
        Notification1.Sound = False
        Notification1.SetInfo("Service SMB", "Action terminée", Main)
        Notification1.AutoCancel = True
        Notification1.Notify(1)

    End If
End Sub
Sub Service_Destroy
End Sub
'fonction delay
Sub Delay(nMilliSecond As Long)As Boolean
Dim nBeginTime As Long
Dim nEndTime As Long
nEndTime = DateTime.Now + nMilliSecond
nBeginTime = DateTime.Now
Do While nBeginTime < nEndTime
nBeginTime = DateTime.Now
'Log(nBeginTime)
If nEndTime < nBeginTime Then
Return True
End If
DoEvents
Loop
End Sub
 

cncncn

Member
Licensed User
I resumed a project and before the person used a service for any dialogue between the tablet and the server. I want to use it is made
 
Upvote 0

cncncn

Member
Licensed User
ok thank you for your help.

I change my program but its still not working. All my functions SMB does not work.

here is my new code:
B4X:
Version=2.71
IconFile=
NumberOfModules=3
Module1=widget
ModuleVisible1=1
Module2=bibli
ModuleVisible2=1
Module3=smbService
ModuleVisible3=1
Package=b4a.example
DoNotOverwriteManifest=True
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>~\n~<supports-screens android:largeScreens="true" ~\n~    android:normalScreens="true" ~\n~    android:smallScreens="true" ~\n~    android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~'End of default text.~\n~AddActivityText(Main,<intent-filter>~\n~<action android:name="com.maximussoft.myprovider.REQUEST"/>~\n~<category android:name="android.intent.category.DEFAULT"/>~\n~</intent.filter>)
UserTypesHint=
NumberOfFiles=1
File1=main.bal
NumberOfLibraries=2
Library1=core
Library2=smb
@EndOfDesignText@
#Region  Project Attributes
    #ApplicationLabel: Modification
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'Version 1.0
'dernière mise à jour faite le 28.04.2014
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Dim usismb As SMB
    'Activity is expected to set URL
    Dim Src,Dest,Fil,Fil2 As String
   
    Dim Action As Int
   
   
    Dim DoneSuccessfully As Boolean
    Dim Notification1 As Notification
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    usismb.Initialize("usismb")
    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = True
   
    Activity.LoadLayout("main")
    Activity.SendToBack

    Dim Dest As String
    Dest=File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt")
   
   
            Src        = File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt")
            Fil    = "jeton.txt"
            Fil2=Fil
            Action    = 400
            StartService(smbService)
           

           
           
    usismb.SetCredentials(bibli.UtilisateurServeur,bibli.MotDePasseServeur,bibli.DomaineServeur)
    DoneSuccessfully= False
   
    If Action = 4000 Then 'copie du fichier texte nommé jeton vers le repertoire
    Log("copie du jeton")
        Log(Dest)
        Log(Fil)
        Log(Src)
        usismb.UploadFile(Dest,Fil,Src,Fil2)'Copie du fichier
    End If   
    If Action = 202 Then' sauvegarde Qualité-Fabrication
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier
       
        ToastMessageShow("Plan Qualité-Fabrication sauvegardé sur le serveur",False)   
    End If
    If Action = 201 Then ' sauvegarde page notation
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier
        ToastMessageShow("page sauvegardé sur le serveur",False)   
    End If
    If Action = 203 Then ' sauvegarde photo
        usismb.uploadFile(Dest,Fil,Src,Fil)'Copie du fichier   
    End If
    If Action = 100 Then
        usismb.DownloadFile(Src,Fil,Dest,Fil)'Copie du fichier
       
    'Copier un fichier de la tablette au serveur
    Else
    'Liste des fichiers
    If(Action = 300 OR Action = 400) Then
        Log("Recherche de fichier")
        usismb.ListFiles(Src,Fil)'Recherche les fichiers
   

   
    Else If Action = 500 Then
        usismb.DeleteFile(Src,Fil)'Suppresion d'un dossier ou fichier
        End If       
    End If
    Log(File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt"))
    usismb.ListFiles(File.ReadString("/storage/sdcard0/ApplicationNGV/Sources","jeton.txt"),"jeton.txt")
    If DoneSuccessfully Then  'Si jeton présent
        ToastMessageShow("modif pas possible",True)
    Else
        Log(Dest)               
        usismb.UploadFile("/storage/sdcard0/ApplicationNGV/Sources",Fil,Dest,Fil2) 'Création du jeton
        ToastMessageShow("modif possible",True)       
    End If
           
           
End Sub

Sub Activity_Resume

    'Activity.Finish
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Top