gpt4 book ai didi

android - 从广播接收器启动的 Activity ,尽管已经完成,但仍保留最近的任务

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:33 27 4
gpt4 key购买 nike

我无法解决两个 Activity 的问题:

我有一个使用警报管理器编写警报的 A Activity ,警报接收器在 3 秒内启动了一个只有完成按钮的 B Activity 。

如果我从 A 对警报管理器进行编程并按后退按钮完成此 Activity ,则 3 秒后会出现 B Activity 。一切正常。问题如下:如果我通过启动器重新打开应用程序,系统会启动 A Activity ,但如果我通过长按主页按钮(最近使用的任务)启动应用程序,我总是打开 B Activity 。我需要当我完成 B Activity 时,如果我从任何地方重新打开应用程序,A Activity 应该打开。

我已尝试将 noHistory 添加到 Activity B,但问题仍然存在。

这是A Activity 的代码:

public class ActivityA extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);

Button b=(Button) findViewById(R.id.initTimer);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
programTimer();
}
});
}

private void programTimer() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReciver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000 , pIntent);
}

B Activity :

public class ActivityB extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);

Button button= (Button) findViewById(R.id.bFinish);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
finish();
}
});
}

报警接收器:

public class AlarmReciver extends android.content.BroadcastReceiver {
private static final String DEBUG_TAG="ReceptorAlarma";
@Override
public void onReceive(Context context, android.content.Intent intent) {
//lanzar activity
Intent i = new Intent(context, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}

list :

    <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityA"
android:label="@string/title_activity_activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ActivityB">
</activity>

<receiver android:name=".AlarmReciver" >
</receiver>
</application>

感谢您的宝贵时间。

最佳答案

你写道:

If I program the alarm manager from A and finish this activity pressing the back button, in 3 seconds the B activity appears. All normal.

注意:您现在有 1 个正在运行的任务,其根 Activity 是 ActivityB。

The problem is the following: if I re-open the application by the launcher, the system starts A activity,

注意:当您从启动器启动应用程序时,这将启动另一个任务,并将 ActivityA 作为其根 Activity 。现在您有 2 个正在运行的任务:一个以 ActivityA 作为根,一个以 ActivityB

...but if I start the application by long pressing the home button (recently used tasks) I always open the B activity.

注意:长按HOME键选择最近使用的任务时,选择的是AlarmReciver启动的任务,它的根是ActivityB

I need that when I finish B activity , if I reopen the application from anywhere, the A activity should open.

您应该能够通过在 <activity> 中设置以下内容来获得您想要的行为ActivityB 的标签:

android:excludeFromRecents="true"

这将确保,当您启动一个以 ActivityB 作为其根 Activity 的任务时,该任务不会显示在最近的任务列表中。这样,启动应用程序的唯一方法是从主屏幕,或者从最近任务列表中选择它(它将以 ActivityA 作为根启动的任务).

关于android - 从广播接收器启动的 Activity ,尽管已经完成,但仍保留最近的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12604620/

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