注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[原创][精华]关于后台程序被安卓系统杀掉的思考  

2017-01-17 18:13:01|  分类: B.调试发布 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
      
      如果你经常在安卓手机上使用“腾讯新闻”,你会发现这样一个有趣的现象:
      启动腾讯新闻”后,进入某篇新闻,把这个“腾讯新闻”转到后台了,然后你又打开多个其它程序。这时可能安卓系统觉得后台程序运行太多,造成内存吃紧,于是在未经你同意的情况下,把一些把后台程序杀掉。如果幸运好,安卓系统把在后台的“腾讯新闻”也杀掉了,当你再次进入“腾讯新闻”,也就是程序由后台转到前台时,你会发觉“腾讯新闻” 又切换到首界面的广告了,过了几秒广告时间,又进入首界面了。很可能“腾讯新闻”在检测到自己被安卓杀掉后,它自动跳到首界面,等于让程序重新启动。

      其实很多安卓程序在后台被杀掉后,都会切换到首界面,可见安卓程序都已注意到这个问题了,作为B4A爱好者的你,是否也留意到这个问题了。

      如果你的B4A程序涉及到多个Activity模块,这个问题(安卓在后台杀掉你的b4a程序)是你必须要面对的,也是需要你去解决的,不然用户可能由于无法忍受程序被杀掉后的界面,而放弃使用你的软件。2016年12月31日,我花了大量时间针对“后台程序被安卓系统杀掉”的问题作了一番研究。下面是我本人为研究后台程序被安卓系统杀掉后的变量,而特地制作的一个小程序。

[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
程序各模块

 
[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
程序各模块


[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
 程序各模块


[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
配置截图

 
[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
配置截图


[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
 File目录中的TXT文件
 
 
[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
 程序运行效果图


 

 ======================== Main 模块代码如下 ========================

#Region  Project Attributes 

#If Default '正式版
    #ApplicationLabel: 检测漰溃
'自定义图标,PNG图片文件作程序图标
' #CustomBuildAction: 1, c:\windows\system32\cmd.exe, /c copy ..\Default.png  res\drawable\icon.png
    
#End If


    #If bro  '辅助版
    #ApplicationLabel: 检测2
' #CustomBuildAction: 1, c:\windows\system32\cmd.exe, /c copy ..\Auxiliary.png res\drawable\icon.png
#End If

#VersionCode: 1
#VersionName: 1.0
'SupportedOrientations 可用值: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
#FullScreen: true
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim pg_i As Int 
Dim s As String

End Sub

Sub Globals
Dim g_i As Int 
'模拟标题栏,去掉B4A自带的标题栏
Dim pg_lbl_Title As Label
Dim pg_edt_Text As EditText
Dim pg_bt1 , pg_bt2 ,pg_bt3 , pg_bt4 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
' 11.txt是以"UTF8"编码形式保存的
s = File.GetText(File.DirAssets  ,"11.txt")    

Dim Data() As Byte

'要在安卓手机上显示汉字就必须用UTF8编码
Data = s.GetBytes("UTF8")

s = BytesToString (Data,0, Data.Length, "UTF8") & CRLF
s = "● " & DateTime.Time(DateTime.now) & " Main FirstTime = " & FirstTime & CRLF & CRLF& CRLF & s
pg_i = pg_i + 1
g_i = g_i + 1
CFunction.Create_TitleBar( Activity ,pg_lbl_Title , "首界面")
Activity.Color = Colors.White
pg_edt_Text.Initialize("")
pg_edt_Text.SingleLine = False
pg_edt_Text.TextColor = Colors.Black
pg_edt_Text.Gravity = Gravity.LEFT + Gravity.TOP
pg_bt1.Initialize("bt1")
pg_bt1.Text = "查看变量"
pg_bt2.Initialize("bt2")
pg_bt2.Text = "A1"
pg_bt3.Initialize("bt3")
pg_bt3.Text = "变量累加"
pg_bt4.Initialize("bt4")
pg_bt4.Text = "自杀"
Activity.AddView(pg_edt_Text ,0,pg_lbl_Title.Height,100%x,100%y-pg_lbl_Title.Height*5)
Activity.AddView(pg_bt1,2.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt2,52.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt3,2.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt4,52.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

End Sub

Sub Activity_Resume
s = "● " & DateTime.Time(DateTime.now) & " Main Resume" & CRLF & s
pg_edt_Text.Text = s 

End Sub

Sub Activity_Pause (UserClosed As Boolean)
s = pg_edt_Text.Text
s = "● " & DateTime.Time(DateTime.now) & " Main UserClosed = " & UserClosed & CRLF & s
' ToastMessageShow("UserClosed = " & UserClosed,False)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
Return False '程序将转到后台
' Return true '程序不作任何响应

End Sub


Sub bt1_Click
Dim s As String = "● " & DateTime.Time(DateTime.now) & CRLF
s = s & "Starter.g_i = " & Starter.pg_i & CRLF
s = s & "Main.pg_i   = " & pg_i & CRLF & CRLF
s = s & "A1.pg_i = " & A1.pg_i & CRLF
s = s & "A2.pg_i = " & A2.pg_i & CRLF
s = s & "A3.pg_i = " & A3.pg_i & CRLF& CRLF
s = s & "Main.g_i  = " & g_i & CRLF& CRLF
pg_edt_Text.Text = s & pg_edt_Text.Text
End Sub

Sub bt2_Click
StartActivity(A1)
End Sub

Sub bt3_Click
Starter.pg_i = Starter.pg_i + 1
pg_i = pg_i +1
A1.pg_i = A1.pg_i +1
A2.pg_i = A2.pg_i +1
A3.pg_i = A3.pg_i +1
g_i = g_i +1
End Sub

Sub bt4_Click
' ExitApplication
Activity.Finish
End Sub


 ======================== Starter 服务模块代码如下 ========================


#Region  Service Attributes 
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region

Sub Process_Globals

Dim pg_i As Int 

End Sub

Sub Service_Create
Main.s = "● " & DateTime.Time(DateTime.now) & " Service_Create" & CRLF & Main.s

End Sub

Sub Service_Start (StartingIntent As Intent)
Main.s = "● " & DateTime.Time(DateTime.now) & " Service_Start" & CRLF & Main.s
pg_i = pg_i + 1
End Sub

'返回 true 以允许 OS 默认的异常处理器来处理未捕捉的异常.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub

Sub Service_Destroy

End Sub



 ======================== CFunction 代码模块代码如下 ========================

'代码模块
'所有模块都可以访问此代码模块中的子程序.
Sub Process_Globals
'当应用程序启动时这些全局变量将被声明一次.
'所有模块都可以访问这些变量.

End Sub

'模拟B4A安卓程序的标题栏,并增加夜视效果
Sub Create_TitleBar(Parent As Activity ,lblTitle As Label ,s As String)

lblTitle.Initialize("Title")
lblTitle.Text = s

lblTitle.Color= Colors.Green
lblTitle.TextColor = Colors.Blue
lblTitle.Gravity=Gravity.CENTER_VERTICAL
Parent.AddView(lblTitle,0,0,100%x,40dip)

End Sub

 ========================  Activity模块 A1  代码如下 ========================

#Region  Activity Attributes 
#FullScreen: true
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim pg_i As Int 

End Sub

Sub Globals
Dim g_i As Int 

'模拟标题栏,去掉B4A自带的标题栏
Dim pg_lbl_Title As Label
Dim pg_edt_Text As EditText
Dim pg_bt1 , pg_bt2 ,pg_bt3 , pg_bt4 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
Main.s = "● " & DateTime.Time(DateTime.now) & " A1 FirstTime = " & FirstTime & CRLF & Main.s
pg_i = pg_i + 1
g_i = g_i + 1

CFunction.Create_TitleBar( Activity ,pg_lbl_Title , "A1")
Activity.Color = Colors.White
pg_edt_Text.Initialize("")
pg_edt_Text.SingleLine = False
pg_edt_Text.TextColor = Colors.Black
pg_edt_Text.Gravity = Gravity.LEFT + Gravity.TOP
pg_bt1.Initialize("bt1")
pg_bt1.Text = "查看变量"
pg_bt2.Initialize("bt2")
pg_bt2.Text = "A2"
pg_bt3.Initialize("bt3")
pg_bt3.Text = "变量累加"
pg_bt4.Initialize("bt4")
pg_bt4.Text = "A3"
Activity.AddView(pg_edt_Text ,0,pg_lbl_Title.Height,100%x,100%y-pg_lbl_Title.Height*5)
Activity.AddView(pg_bt1,2.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt2,52.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt3,2.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt4,52.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)


' pg_lbl_Title.Text = "A1 FirstTime = " & FirstTime

End Sub

Sub Activity_Resume
Main.s = "● " & DateTime.Time(DateTime.now) & " A1 Resume" & CRLF & Main.s
pg_edt_Text.Text = Main.s 

End Sub

Sub Activity_Pause (UserClosed As Boolean)
' ToastMessageShow("UserClosed = " & UserClosed,False)
Main.s = pg_edt_Text.Text
Main.s = "● " & DateTime.Time(DateTime.now) & " A1 UserClosed = " & UserClosed & CRLF & Main.s
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
StartActivity(Main)

'StartActivity 代码执行后返回true 则Activity_Pause 事件参数将是false
Return False
End Sub


Sub bt1_Click
Dim s As String = "● " & DateTime.Time(DateTime.now) & CRLF
s = s & "Starter.g_i = " & Starter.pg_i & CRLF
s = s & "Main.pg_i  = " & Main.pg_i & CRLF & CRLF
s = s & "A1.pg_i = " & pg_i & CRLF
s = s & "A2.pg_i = " & A2.pg_i & CRLF
s = s & "A3.pg_i = " & A3.pg_i & CRLF& CRLF
s = s & "A1.g_i = " & g_i & CRLF& CRLF
pg_edt_Text.Text = s & pg_edt_Text.Text
End Sub

Sub bt2_Click
StartActivity(A2)
End Sub

Sub bt3_Click
Starter.pg_i = Starter.pg_i + 1
Main.pg_i = Main.pg_i +1
A3.pg_i = A3.pg_i +1
A2.pg_i = A2.pg_i +1
pg_i = pg_i +1
g_i = g_i +1
End Sub

Sub bt4_Click
StartActivity(A3)
End Sub


 ========================  Activity模块 A2  代码如下 ========================

#Region  Activity Attributes 
#FullScreen: true
#IncludeTitle: True
#End Region

Sub Process_Globals

Dim pg_i As Int 

End Sub

Sub Globals

Dim g_i As Int 

'模拟标题栏,去掉B4A自带的标题栏
Dim pg_lbl_Title As Label
Dim pg_edt_Text As EditText
Dim pg_bt1 , pg_bt2 ,pg_bt3 , pg_bt4 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
Main.s = "● " & DateTime.Time(DateTime.now) & " A2 FirstTime = " & FirstTime & CRLF & Main.s
pg_i = pg_i + 1
g_i = g_i + 1
CFunction.Create_TitleBar( Activity ,pg_lbl_Title , "A2")
Activity.Color = Colors.White
pg_edt_Text.Initialize("")
pg_edt_Text.SingleLine = False
pg_edt_Text.TextColor = Colors.Black
pg_edt_Text.Gravity = Gravity.LEFT + Gravity.TOP
pg_bt1.Initialize("bt1")
pg_bt1.Text = "查看变量"
pg_bt2.Initialize("bt2")
pg_bt2.Text = "A3"
pg_bt3.Initialize("bt3")
pg_bt3.Text = "变量累加"
pg_bt4.Initialize("bt4")
pg_bt4.Text = "回首页"
Activity.AddView(pg_edt_Text ,0,pg_lbl_Title.Height,100%x,100%y-pg_lbl_Title.Height*5)
Activity.AddView(pg_bt1,2.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt2,52.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt3,2.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt4,52.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)


' pg_lbl_Title.Text = "A2 FirstTime = " & FirstTime


End Sub

Sub Activity_Resume
Main.s = "● " & DateTime.Time(DateTime.now) & " A2 Resume" & CRLF & Main.s
pg_edt_Text.Text = Main.s 
End Sub

Sub Activity_Pause (UserClosed As Boolean)
' ToastMessageShow("UserClosed = " & UserClosed,False)
Main.s = pg_edt_Text.Text
Main.s = "● " & DateTime.Time(DateTime.now) & " A2 UserClosed = " & UserClosed & CRLF & Main.s
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
StartActivity(A1)

'StartActivity 代码执行后返回true 则Activity_Pause 事件参数将是false
Return True
End Sub

Sub bt1_Click
Dim s As String = "● " & DateTime.Time(DateTime.now) & CRLF
s = s & "Starter.g_i = " & Starter.pg_i & CRLF
s = s & "Main.pg_i  = " & Main.pg_i & CRLF & CRLF
s = s & "A1.pg_i = " & A1.pg_i & CRLF
s = s & "A2.pg_i = " & pg_i & CRLF
s = s & "A3.pg_i = " & A3.pg_i & CRLF& CRLF
s = s & "A2.g_i = " & g_i & CRLF& CRLF

pg_edt_Text.Text = s & pg_edt_Text.Text
End Sub

Sub bt2_Click
StartActivity(A3)
End Sub

Sub bt3_Click
Starter.pg_i = Starter.pg_i + 1
Main.pg_i = Main.pg_i +1
A3.pg_i = A3.pg_i +1
A1.pg_i = A1.pg_i +1
pg_i = pg_i +1
g_i = g_i +1
End Sub

Sub bt4_Click
StartActivity(Main)
End Sub


 ========================  Activity模块 A3  代码如下 ========================

#Region  Activity Attributes 

#FullScreen: true
#IncludeTitle: True
#End Region

Sub Process_Globals

Dim pg_i As Int 

End Sub

Sub Globals
Dim g_i As Int

'模拟标题栏,去掉B4A自带的标题栏
Dim pg_lbl_Title As Label
Dim pg_edt_Text As EditText
Dim pg_bt1 , pg_bt2 ,pg_bt3 , pg_bt4 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
Main.s = "● " & DateTime.Time(DateTime.now) & " A3 FirstTime = " & FirstTime & CRLF & Main.s
pg_i = pg_i + 1
g_i = g_i + 1

CFunction.Create_TitleBar( Activity ,pg_lbl_Title , "A3")
Activity.Color = Colors.White
pg_edt_Text.Initialize("")
pg_edt_Text.SingleLine = False
pg_edt_Text.TextColor = Colors.Black
pg_edt_Text.Gravity = Gravity.LEFT + Gravity.TOP
pg_bt1.Initialize("bt1")
pg_bt1.Text = "查看变量"
pg_bt2.Initialize("bt2")
pg_bt2.Text = "首界面"
pg_bt3.Initialize("bt3")
pg_bt3.Text = "变量累加"
pg_bt4.Initialize("bt4")
pg_bt4.Text = "自杀"
Activity.AddView(pg_edt_Text ,0,pg_lbl_Title.Height,100%x,100%y-pg_lbl_Title.Height*5)
Activity.AddView(pg_bt1,2.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt2,52.5%x,pg_edt_Text.Top + pg_edt_Text.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt3,2.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)

Activity.AddView(pg_bt4,52.5%x,pg_bt1.Top + pg_bt1.Height + pg_lbl_Title.Height/3, 45%x,pg_lbl_Title.Height)


' pg_lbl_Title.Text = "A3 FirstTime = " & FirstTime

End Sub

Sub Activity_Resume
Main.s = "● " & DateTime.Time(DateTime.now) & " A3 Resume" & CRLF & Main.s  
pg_edt_Text.Text = Main.s 
End Sub

Sub Activity_Pause (UserClosed As Boolean)
' ToastMessageShow("UserClosed = " & UserClosed ,False)
Main.s = pg_edt_Text.Text
Main.s = "● " & DateTime.Time(DateTime.now) & " A3 UserClosed = " & UserClosed & CRLF & Main.s
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean

StartActivity(A2)

'StartActivity 代码执行后返回true 则Activity_Pause 事件参数将是false
Return True
End Sub


Sub bt1_Click
Dim s As String = "● " & DateTime.Time(DateTime.now) & CRLF
s = s & "Starter.g_i = " & Starter.pg_i & CRLF
s = s & "Main.pg_i  = " & Main.pg_i & CRLF & CRLF
s = s & "A1.pg_i = " & A1.pg_i & CRLF
s = s & "A2.pg_i = " & A2.pg_i & CRLF
s = s & "A3.pg_i = " & pg_i & CRLF& CRLF
s = s & "A3.g_i  = " & g_i & CRLF& CRLF

pg_edt_Text.Text = s & pg_edt_Text.Text
End Sub

Sub bt2_Click
StartActivity(Main)
End Sub

Sub bt3_Click
Starter.pg_i = Starter.pg_i + 1
Main.pg_i = Main.pg_i +1
A1.pg_i = A1.pg_i +1
A2.pg_i = A2.pg_i +1
pg_i = pg_i +1
g_i = g_i +1

End Sub

Sub bt4_Click
StartActivity(Main)
Activity.finish
End Sub


 ======================== 解决方案 ========================

[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
在Main模块的Process_Globals中定义一个int类型变量
 
[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
在Main模块的Activity_Create事件中改变这个变量的值

 
[原创][精华]关于后台程序被安卓系统杀掉的思考1 - 沉默蜂 - 沉默蜂B4A安卓编程
如果程序在后台被安卓系统杀掉后,可能会进入原来的activity模块的 Activity_Create 事件,当发现值变成0时,就算检测到它之前在后台被安卓系统杀掉了。也有可能从后台转到前台后,程序会直接跳到 main模块的Activity_Create事件。
 
  评论这张
 
阅读(126)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017