Android 如何在没有 UI 的情况下启动 Activity?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2704084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:01:11  来源:igfitidea点击:

How to launch an Activity without a UI?

android

提问by fjmustak

Is it in any way possible to launch an activity from the main function without having a UI? i.e. is there a way to create a sort of "wrapper" around another activity, i.e. by launching the main activity, it takes you to another activity automatically.

是否可以在没有 UI 的情况下从主功能启动活动?即有没有办法围绕另一个活动创建一种“包装器”,即通过启动主要活动,它会自动将您带到另一个活动。

If that is not possible, is there a way to remove the main activity from the stack so that clicking the back button does not take you to a blank UI? Here's an example of what I'm trying to do:

如果这是不可能的,有没有办法从堆栈中删除主要活动,以便单击后退按钮不会将您带到空白 UI?这是我正在尝试做的一个例子:

public class WrapperActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:555-1212"));
        startActivity(intent);
    }
}

采纳答案by Vishwanath

You need to add the Intent flag,

您需要添加 Intent 标志,

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Or

或者

call "finish();" after firing the intent.

finish();在触发意图后调用“ ”。

回答by Justin

Android also provides a theme specifically for this:

Android 还专门为此提供了一个主题:

android:theme="@android:style/Theme.NoDisplay"

回答by strange quark

In your manifest, when you declare the activity, use theme "@android:style/Theme.Translucent.NoTitleBar"

在您的清单中,当您声明活动时,请使用主题 "@android:style/Theme.Translucent.NoTitleBar"

Ex:

前任:

<activity android:name="yourActivityName" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar">

回答by arunwithasmile

Just in case you are using Android 6.0+ or Target SDK is 23+, having a theme android:theme = "@android:style/Theme.NoDisplay"will lead to a crash with error did not call finish() prior to onResume() completing. This in fact is a bug recognised by Google developers here.

以防万一您使用的是 Android 6.0+ 或 Target SDK 是 23+,拥有主题android:theme = "@android:style/Theme.NoDisplay"会导致崩溃并出现错误did not call finish() prior to onResume() completing。这实际上是 Google 开发人员在此处承认的错误。

So it is recommended to use an activity with following theme as a workaround.

因此,建议使用具有以下主题的活动作为解决方法。

android:theme = "@android:style/Theme.Translucent.NoTitleBar"

android:theme = "@android:style/Theme.Translucent.NoTitleBar"

回答by Tmac

Using

使用

<activity android:name="yourActivityName" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar">

mentioned by Brian515 works great. This method is useful for creating an entry point Activity that decides on which activity to call, start, services, etc without having to show a UI to the user. Remember to use finish()after you have started your intent.

Brian515 提到的效果很好。此方法对于创建一个入口点 Activity 非常有用,该入口点 Activity 决定要调用、启动、服务等的 Activity,而无需向用户显示 UI。请记住finish()在开始意图后使用。

回答by Fermin Genao

I think this would help you a lot:

我认为这会对你有很大帮助:

<activity  android:name = "MyActivity" 
          android:label = "@string/app_name" 
          android:theme = "@android:style/Theme.NoDisplay" >

回答by Vipul Behl

In your manifest add @android:style/Theme.Translucent.NoTitleBar"as mentioned in some of the answers above.

在您的清单中添加@android:style/Theme.Translucent.NoTitleBar"上面一些答案中提到的内容。

Also remove the setContentView(R.layout.your_activity);line from your activity.java file.

同时setContentView(R.layout.your_activity);从您的 activity.java 文件中删除该行。

回答by Sreedevi J

Looks similar to the question asked here: Removing an activity from the history stack

看起来类似于这里提出的问题:Removing an activity from the history stack

If it is, then you can use:

如果是,那么您可以使用:

FLAG_ACTIVITY_NO_HISTORY

FLAG_ACTIVITY_NO_HISTORY

This should work to wipe activities off of the stack.

这应该可以清除堆栈中的活动。

If you need to exclude from recent apps (long press home key) you can use this flag:

如果您需要从最近的应用程序中排除(长按主页键),您可以使用此标志:

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

回答by sfratini

If you are not interacting with the UI, what you are trying to do sounds more like an android service.

如果您不与 UI 交互,那么您尝试做的事情听起来更像是一个 android 服务。

回答by Tarak

I had used moveTaskToBack(true)in onResume()to put the entire activity stack in background.

我曾用moveTaskToBack(true)onResume()把整个活动堆栈的背景。