gpt4 book ai didi

java - 我可以从启动启动一个 Activity 并让它进入后台而不让用户看到它吗,android

转载 作者:行者123 更新时间:2023-12-01 15:44:46 25 4
gpt4 key购买 nike

目前,我有代码可以从启动时启动应用程序,但将其打开到前台。这是由

完成的
 public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent start = new Intent(context, ApolloMobileActivity.class);
start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(start);

然后为了在启动时将其发送到后台,我创建了另一个名为 StartAtBootService 的 java 文件所以我将接收器类更改为:

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.ssab.StartAtBootService");
context.startService(i);
}

服务类是

 public class StartAtBootService extends Service 
{
public IBinder onBind(Intent intent)
{
return null;
}

@Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "StartAtBootService Created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");

// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

/*
* In Android 2.0 and later, onStart() is depreciated. Use
* onStartCommand() instead, or compile against API Level 5 and
* use both.
* http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
@Override
public void onStart(Intent intent, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
}
*/

@Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
}
}

是否可以更改 StartAtBootService 以在后台运行另一个名为 ApolloMobileActivity 的 java 文件中的 Activity ?我已经测试了这段代码,即使它在启动时在后台运行,它也不会运行 ApolloMobileActivity 中的代码。

请帮忙!谢谢大家:)

最佳答案

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.

来自 Activities

你可以启动一个Activity,但是不存在隐形的Activity。这些是 UI 组件。如果你想在后台做一些看不见的工作,你就必须在你的服务中做。

关于java - 我可以从启动启动一个 Activity 并让它进入后台而不让用户看到它吗,android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285846/

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