当任务进入后台或进入前台时回调?
Callback when task goes into background or comes into foreground?
我有一个 activity A,它会启动自定义选项卡。我需要知道自定义选项卡打开时,任务(activity 是其中的一部分)是转到后台还是转到前台。
我知道这个问题 How to detect when an Android app goes to the background and come back to the foreground。这个问题提到的解决方案对我不起作用,因为一旦启动自定义选项卡,就会收到后台回调,这不是我想要的。当包含 activity A 的任务进入后台时,我想要 onbackground 回调。
您的 activity 和 chrome 自定义选项卡之间的关系取决于启动模式。您可以在当前堆栈或新堆栈中启动自定义选项卡。
使用 CustomTabsCallback,您可以在 Tab 隐藏(进入后台)时使用 TAB_HIDDEN
回调或当 Tab 可见(进入前台)时使用 TAB_SHOWN
回调.
来自文档:
TAB_HIDDEN
Sent when the tab becomes hidden.
TAB_SHOWN
Sent when the tab becomes visible.
以下是如何使用上述回调的完整工作示例:
public class CustomTabsActivity extends AppCompatActivity {
private CustomTabsServiceConnection mCustomTabsServiceConnection;
private CustomTabsClient mCustomTabsClient;
private CustomTabsSession mCustomTabsSession;
private CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customTabsButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomTabs();
}
});
initCustomTabs();
}
@Override
protected void onStart() {
super.onStart();
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void initCustomTabs() {
mCustomTabsServiceConnection = new CustomTabsServiceConnection()
{
@Override
public void onCustomTabsServiceConnected(@NotNull ComponentName componentName, @NotNull CustomTabsClient customTabsClient)
{
mCustomTabsClient = customTabsClient;
mCustomTabsClient.warmup(0L);
mCustomTabsSession = mCustomTabsClient.newSession(new CustomTabsCallback()
{
@Override
public void onNavigationEvent(int navigationEvent, Bundle extras) {
switch (navigationEvent)
{
case CustomTabsCallback.TAB_SHOWN:
//Sent when the tab becomes visible (goes into foreground)
break;
case CustomTabsCallback.TAB_HIDDEN:
//Sent when the tab becomes hidden (goes into background)
break;
}
}
});
builder.setSession(mCustomTabsSession);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mCustomTabsClient = null;
}
};
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}
private void showCustomTabs(){
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://whosebug.com/"));
}
}
我有一个 activity A,它会启动自定义选项卡。我需要知道自定义选项卡打开时,任务(activity 是其中的一部分)是转到后台还是转到前台。
我知道这个问题 How to detect when an Android app goes to the background and come back to the foreground。这个问题提到的解决方案对我不起作用,因为一旦启动自定义选项卡,就会收到后台回调,这不是我想要的。当包含 activity A 的任务进入后台时,我想要 onbackground 回调。
您的 activity 和 chrome 自定义选项卡之间的关系取决于启动模式。您可以在当前堆栈或新堆栈中启动自定义选项卡。
使用 CustomTabsCallback,您可以在 Tab 隐藏(进入后台)时使用 TAB_HIDDEN
回调或当 Tab 可见(进入前台)时使用 TAB_SHOWN
回调.
来自文档:
TAB_HIDDEN
Sent when the tab becomes hidden.
TAB_SHOWN
Sent when the tab becomes visible.
以下是如何使用上述回调的完整工作示例:
public class CustomTabsActivity extends AppCompatActivity {
private CustomTabsServiceConnection mCustomTabsServiceConnection;
private CustomTabsClient mCustomTabsClient;
private CustomTabsSession mCustomTabsSession;
private CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customTabsButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomTabs();
}
});
initCustomTabs();
}
@Override
protected void onStart() {
super.onStart();
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void initCustomTabs() {
mCustomTabsServiceConnection = new CustomTabsServiceConnection()
{
@Override
public void onCustomTabsServiceConnected(@NotNull ComponentName componentName, @NotNull CustomTabsClient customTabsClient)
{
mCustomTabsClient = customTabsClient;
mCustomTabsClient.warmup(0L);
mCustomTabsSession = mCustomTabsClient.newSession(new CustomTabsCallback()
{
@Override
public void onNavigationEvent(int navigationEvent, Bundle extras) {
switch (navigationEvent)
{
case CustomTabsCallback.TAB_SHOWN:
//Sent when the tab becomes visible (goes into foreground)
break;
case CustomTabsCallback.TAB_HIDDEN:
//Sent when the tab becomes hidden (goes into background)
break;
}
}
});
builder.setSession(mCustomTabsSession);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mCustomTabsClient = null;
}
};
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}
private void showCustomTabs(){
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://whosebug.com/"));
}
}