Bra*_*rad 28 notifications android android-activity
我有一个前台服务,只要用户登录到应用程序,就会保持与服务器的连接.这样即使用户按Home键将应用程序发送到后台,连接仍然保持活动并且可以直接从服务器接收消息.
该应用程序有许多活动,当它们被发送到后台时,其中任何活动都可以是活动的.
我想允许用户单击通知以恢复当前活动.我了解如何恢复特定活动,但想知道是否有办法恢复用户所在的最后一个活动?当然我可以跟踪最后一个,然后从Notification回调中调用它,但是认为在任务级别可能有办法?
感谢您提供的任何建议.
Dav*_*ser 64
你需要的只是一个什么都不做的简单活动.这是一个例子:
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Now finish, which will drop the user in to the activity that was at the top
// of the task stack
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
设置通知以开始此活动.确保在清单中,此活动的任务关联性与应用程序中其他活动的任务关联性相同(默认情况下,如果您未明确设置android:taskAffinity).
当用户选择此通知时,如果您的应用程序正在运行,那么NotificationActivity将在应用程序任务中最顶层的活动之上启动,该任务将被带到前台.当NotificationActivity完成时,它将简单地将用户返回到应用程序中的最顶层活动(即:用户在进入后台时将其保留在何处).
如果您的应用程序尚未运行,则无法使用此功能.但是,您有两个选项可以处理:
确保应用程序未运行时通知栏中不存在通知.
在NotificationActivity的onCreate()方法中,检查您的应用程序是否正在运行,如果它没有运行,请调用startActivity()并启动您的应用程序.如果这样做,请确保在启动应用程序时设置标志Intent.FLAG_ACTIVITY_NEW_TASK,以便任务的根活动不是NotificationActivity.
Rag*_*ari 29
工作得很好,谢谢大卫!以下类检查应用程序是否已在运行,如果没有,则在完成之前启动它(如David在选项2中所建议的那样).
public class NotificationActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// If this activity is the root activity of the task, the app is not running
if (isTaskRoot())
{
// Start the app before finishing
Intent startAppIntent = new Intent(getApplicationContext(), MainActivity.class);
startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startAppIntent);
}
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14249 次 |
| 最近记录: |