如何正确地将活动与新任务联系起来?(How to properly chain activities with new tasks?)

编程入门 行业动态 更新时间:2024-10-24 22:25:57
如何正确地将活动与新任务联系起来?(How to properly chain activities with new tasks?)

我正在报警申请。 我有3个活动: Main , Alarm , Game 。

它们在清单中声明:

<activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize" android:screenOrientation="sensorPortrait" > <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".Alarm" android:excludeFromRecents="true" android:launchMode="singleTask" android:screenOrientation="sensorPortrait" /> <activity android:name=".Game" android:excludeFromRecents="true" android:label="@string/search_word" android:launchMode="singleTask" android:screenOrientation="sensorPortrait" />

从broadcastreciever调用Alarm活动:

Intent i = new Intent(context, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i);

或者来自`Game's onBackPressed:

if (!test) { Intent i = new Intent(mContext, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } finish();

以类似的方式从Alarm活动调用Game活动:

i = new Intent(this, BubblePopActivity.class); i.putExtras(b); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i);

Main活动没有内部调用。

行为我想要实现:

1)活动Alarm和Game应显示在锁定屏幕上,解锁后用针,图形等方式锁定。 它适用于活动中的标志十六进制,这意味着需要FLAG_NEW_TASK

super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_bgame);

2)后退按钮应该从Game活动到Alarm活动3)如果按下了home ,当Alarm或Game活动在堆栈顶部时,启动器图标应该重新打开它们,但不能重新打开,只要这些活动没有完成应用逻辑。 虽然1)和2)流畅地工作,但3)偶尔工作。 有时它会返回Game of Alarm活动。 看起来如果这些活动由于缺乏资源而被系统杀死,启动器图标将导致Main活动。 我如何实现所需的逻辑?

I am making alarm application. I have 3 Activities: Main, Alarm,Game.

They are declared in manifest:

<activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize" android:screenOrientation="sensorPortrait" > <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".Alarm" android:excludeFromRecents="true" android:launchMode="singleTask" android:screenOrientation="sensorPortrait" /> <activity android:name=".Game" android:excludeFromRecents="true" android:label="@string/search_word" android:launchMode="singleTask" android:screenOrientation="sensorPortrait" />

Alarm activity is called from broadcastreciever:

Intent i = new Intent(context, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i);

Or from `Game's onBackPressed:

if (!test) { Intent i = new Intent(mContext, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } finish();

Game activity is called from Alarm activity in similar way:

i = new Intent(this, BubblePopActivity.class); i.putExtras(b); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i);

There is no internal calls to Main activity.

Behaiviour I am tring to achieve:

1) Activities Alarm and Game should be shown over lockscreen both unlocked nad locked with pin, grafical pattern and so on. It works with flags decrared in activities, which requires FLAG_NEW_TASK in intent

super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_bgame);

2) Back button should lead from Game activity to Alarm activity 3) If home is pressed, while Alarm or Game activity is on top of stack, launcher icon should reopen them, but not the Main, as long as those activities are not finished by application logic. While 1) and 2) works flowlesly, 3) works occasionnaly. Sometimes it returns to Game of Alarm activity as it should. It looks like if those activities were killed by system due to lack of resourses, launcher icon will lead to Main activity. How can I implement desired logic?

最满意答案

我添加了sharede偏好常量。 当我启动主要活动时,我会检查用户是否已解除警报,然后正常启动,否则启动警报活动。

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean serviceIsRunning = isMyServiceRunning(AlarmService.class); if (serviceIsRunning) { boolean alarmRings = RingingAlarmUtil.getRingingAlarm(this).isExpired(); if (!alarmRings) { Intent i = new Intent(this, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); finish(); } } setContentView(R.layout.activity_main); ButterKnife.bind(this); //normal launch staff }

I added sharede preference constants. When I launch main activity, I check if alarm was dissmissed by user, then launch as normal, else launch alarm activity.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean serviceIsRunning = isMyServiceRunning(AlarmService.class); if (serviceIsRunning) { boolean alarmRings = RingingAlarmUtil.getRingingAlarm(this).isExpired(); if (!alarmRings) { Intent i = new Intent(this, NotificationActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); finish(); } } setContentView(R.layout.activity_main); ButterKnife.bind(this); //normal launch staff }

更多推荐

本文发布于:2023-08-05 18:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确地   新任务   properly   tasks   activities

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!