安装后打开和完成的应用程序的不同行为成功(Different behavior of app in open and done after installation succefull)

编程入门 行业动态 更新时间:2024-10-28 02:36:40
安装后打开和完成的应用程序的不同行为成功(Different behavior of app in open and done after installation succefull)

我的Android应用程序曾经在后台被杀,即使有前台服务。 这是服务的清单条目:

<service android:name=".MyService" android:icon="@drawable/icon" android:label="TritonHK" android:process=":my_process" > </service>

这是服务的代码

MLog.w(getClass().getName(), "TritonHK started"); Notification note=new Notification(R.drawable.icon, "TritonHK is running", System.currentTimeMillis()); Intent i=new Intent(this, BGMessages.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0); note.setLatestEventInfo(this, "TritonHK", "TritonHK", pi); note.flags|=Notification.FLAG_NO_CLEAR; startForeground(1337, note);

以下是我开始服务的方式:

Intent i=new Intent(this, MyService.class); startService(i);

我正在onCreate开始我的第一个活动服务。

我已经通过从清单中的服务中删除android:process=":my_process"来解决这个问题,现在它看起来像这样:

<service android:name=".MyService" android:icon="@drawable/icon" android:label="TritonHK" > </service>

但现在我面临一个有趣的问题。

当我在我的设备上安装应用程序并在安装成功后,我点击done并从图标启动我的应用程序,它运行正常。

但安装成功后如果我通过单击open按钮启动应用程序它第一次在后台被杀死。 然后,如果我强制关闭应用程序并再次从图标启动它,那么它运行正常。

出了什么问题,我很困惑。 请帮帮我

My android app used to got killed in background even with foreground service. Here is the manifest entry for the service :

<service android:name=".MyService" android:icon="@drawable/icon" android:label="TritonHK" android:process=":my_process" > </service>

and here is the code for service

MLog.w(getClass().getName(), "TritonHK started"); Notification note=new Notification(R.drawable.icon, "TritonHK is running", System.currentTimeMillis()); Intent i=new Intent(this, BGMessages.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0); note.setLatestEventInfo(this, "TritonHK", "TritonHK", pi); note.flags|=Notification.FLAG_NO_CLEAR; startForeground(1337, note);

and here is how I am starting my service:

Intent i=new Intent(this, MyService.class); startService(i);

I am starting the service in onCreate of my first activity.

I have over come this bug by removing android:process=":my_process" from my service in manifest, now it looks like this:

<service android:name=".MyService" android:icon="@drawable/icon" android:label="TritonHK" > </service>

But now I am facing an interesting problem.

When I install the app on my device and after installation successful I click on done and launch my app from the icon, It runs fine.

But after installation successful If I launch the app by clicking open button It got killed in background for very first time. Then If I force close the app and launch it from icon again then it runs fine.

I am puzzled what is going wrong. Please help me

最满意答案

服务因资源情况而被杀死。 如果安装程序仍在运行,它当然具有高优先级,因为它对用户可见。 从安装程序打开时,您是否在启动服务时检查了内存消耗?

I have resolved the problem with the following code and posting the same so that others can also use this.

I have added below code to the on create of my launcher activity.

if (!isTaskRoot()) { final Intent intent = getIntent(); final String intentAction = intent.getAction(); if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) { finish(); } }

更多推荐

本文发布于:2023-07-31 09:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   behavior   app   succefull   installation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!