gpt4 book ai didi

android - 如何在启动智能手机时启动 android 应用程序并使其保持运行直到用户将其关闭?

转载 作者:太空狗 更新时间:2023-10-29 15:36:20 25 4
gpt4 key购买 nike

我要求我的应用程序在用户打开智能手机时启动,然后让它在后台运行。有什么先决条件?我怎样才能让它成为可能?谢谢

最佳答案

Activity 不能也不应该在后台运行。对于 Android 中的后台操作,您应该使用 Service

如需更多帮助,请查看此链接-

Running activity in the background

要在启动时启动服务,请添加以下代码-

添加权限-

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

添加接收者-

<receiver
android:name="com.<!your activity!>"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

在您的 Activity 类中-

public class MyReceiver extends BroadcastReceiver {   
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourService.class);
context.startService(myIntent);
}

对于长时间运行的后台操作,应该使用 Service。

关于android - 如何在启动智能手机时启动 android 应用程序并使其保持运行直到用户将其关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20701418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com