1

我正在尝试了解 Chrome 自定义标签的工作原理。按照指南,将自定义项添加到菜单是使用 pendingIntent 完成的,例如:

Intent intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pendingIntent =  PendingIntent
        .getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
customTabsIntent = new CustomTabsIntent.Builder()
        .addMenuItem("Visit Google Website", pendingIntent)
        .build();

我知道如何使用 pendingIntent 指向网站或创建“共享”操作。问题是是否可以通过添加“关闭”菜单项来关闭活动或退出应用程序来执行更复杂的任务?或将该菜单项“连接”到一个方法,因此当用户单击它时,该方法中的代码将被处理。我想如果可能的话,第二个比第一个更容易解决。

我试图寻找答案,但如果我使用意图或pendingIntent 搜索如何关闭或结束活动或应用程序(或类似),我会使用'finish();'得到一些东西 或'startActivity(intent);' 或类似的,我不能在这里申请,因为我不知道如何或从哪里调用它。我尝试阅读有关意图和pendingIntent 的使用,但我无法弄清楚如何,比如说关闭活动或流程而无法调用方法。

我会很感激任何帮助,甚至是一些指向资源的链接或指向我接下来需要阅读/学习的内容才能理解这一点。

4

1 回答 1

2

此解决方案使用 BrodcastReceiver:

在您启动 Chrome 自定义选项卡 (CCT) 的活动(在本例中为 MainActivity)中,创建一个静态字段以识别该活动是否是从 Broadcastreceiver 创建的,并创建一个 setter 以便我们可以从 Broadcastreceiver 设置它:

    // For determining if Activity was started from BroadcastReceiver
    private static boolean fromBroadcastReceiver = false;

    public static void setFromBroadcastReceiver(boolean bool) {
        fromBroadcastReceiver = bool;
    }

为 BroadcastReceiver 创建一个公共类。覆盖 onReceive 方法,创建 MainActivity 的实例并设置fromBroadcastReceiver为 true。然后使用该活动创建一个意图和另一个使用第一个意图重新启动的意图。关闭 CCT 并使用后一个意图重新启动活动:

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.IntentCompat;

public class CctBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Create an instance of MainActivity and set fromBroadcastReceiver flag to true.
        MainActivity mainActivity = new MainActivity();
        mainActivity.setFromBroadcastReceiver(true);

        // Create an intent with that activity and another intent for restarting using first intent.
        Intent intent = new Intent(context, mainActivity.getClass());
        ComponentName compName = intent.getComponent();
        Intent mainIntent = IntentCompat.makeRestartActivityTask(compName);

        // Restart the activity using later intent (also close CCT)
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(mainIntent);

    } // End onReceive.

} // End BroadcastReceiver class.

不要忘记在 AndroidManifest.xml 中注册您的接收器:

...
        </activity>

    <receiver
        android:name=".cctBroadcastReceiver"
        android:enabled="true">
    </receiver>

</application>

现在,在onCreateMainActivity 内部检查它是否是从 BroadcastReceiver 创建的,如果是,则重置“标志”和finish()活动:

        // Check if activity was created from the BroadcastReceiver, and if so reset the 'flag' and finish() the activity.
        if (fromBroadcastReceiver) {
            setFromBroadcastReceiver(false);
            finish();
            return;
        }

不要忘记使用 BroadcastReceiver 类为 CCT 创建意图和 pendingIntent:

     Intent broadcastIntent =  new Intent(this, CctBroadcastReceiver.class);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

你完成了。基本上,BroadcastReceiver 中的代码会关闭 CCT(类似于单击 CCT 默认关闭按钮)。在 MainActivity 中添加“标志”和代码会进一步关闭 MainActivity。

于 2016-05-10T21:24:22.223 回答