Android如何在启动新实例时杀死以前的应用程序实例

标签 android process state launching-application android-launcher

我使用了 android:launchMode="singleInstance",它停止创建应用程序的另一个实例,同时保留第一个实例。

这是我的 Activity 的配置信息

<activity
        android:name="com.abc.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.ABC"
        >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data
                android:host="192.168.0.52"
                android:scheme="http" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

最佳答案

听起来您正在尝试重新启动您的应用。您可以通过在不久的将来设置闹钟来启动您的应用程序并同时终止现有进程来做到这一点。

这可以通过以下代码来实现:

AlarmManager alarm = (AlarmManager) MyActivity.this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(MyActivity.this, 0, new Intent(MyActivity.this, MyActivity.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(), android.os.Process.SIGNAL_KILL);

关于Android如何在启动新实例时杀死以前的应用程序实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21130051/

相关文章:

android - NDK版本与Android版本的关系

Android 从 Widget 调用 Activity 特定功能

后跟 exec 时的克隆、 fork 、vfork 行为

Android 蓝牙在方向改变后崩溃

android - 在 Android 应用程序中隐藏列表

android - ListView OnFocusChangeListener

asp.net - 使用 System.Diagnostics.Process 远程执行进程

我们可以在创建进程后立即以编程方式将进程附加到 VS 吗?

state - 如何从 Flutter 中的其他 StatefulWidget 设置/更新 StatefulWidget 的状态?

javascript - 重置 React JS 的状态