Sub getPID As Int
Dim jo As JavaObject
jo.InitializeStatic("android.os.Process")
Dim pidstr As Int = jo.RunMethod("myPid",Null)
Log("MyPID: " & pidstr)
Return pidstr
End Sub
Sub KillProcess(pid As Int)
Dim jo As JavaObject
jo.InitializeStatic("android.os.Process")
jo.RunMethod("killProcess",Array(pid))
End Sub
Sub attached_check
'**** CODE B4X
Try
Dim filename As String
Dim pid As Int = getPID
filename = $"/proc/${pid}/status"$
Dim TextReader1 As TextReader
TextReader1.Initialize(File.OpenInput("", filename))
Dim line As String
line = TextReader1.ReadLine
Do While Not(line.Contains("TracerPid"))
Log(line) 'write the line to LogCat
line = TextReader1.ReadLine
Loop
TextReader1.Close
Dim splitta() As String = Regex.Split("\t",line)
Log(splitta(1))
If splitta(1) <> 0 Then
'Debug is Attached -- Kill PID
KillProcess(pid)
End If
Catch
Log(LastException)
End Try
'**** CODE C++
' Try
' {
' const int bufsize = 1024;
' char filename[bufsize];
' char line[bufsize];
' int pid = getPID();
' sprintf(filename, "/proc/%d/status", pid);
' File* fd = fopen(filename, "r");
' If (fd != nullptr)
' {
' While (fgets(line, bufsize, fd))
' {
' If (strncmp(line, "TracerPid", 9) == 0)
' {
' int statue = atoi(&line[10]);
' LOGD("%s", line);
' If (statue != 0)
' {
' LOGD("be attached !! kill %d", pid);
' fclose(fd);
' int ret = kill(pid, SIGKILL);
' }
' break;
' }
' }
' fclose(fd);
' } Else
' {
' LOGD("open %s fail...", filename);
' }
' } Catch (...)
' {
'
' }
End Sub