【问题标题】:How to stop an activity in android using intent?如何使用意图停止android中的活动?
【发布时间】:2013-01-19 05:36:47
【问题描述】:

我认为这是一个基本问题。是否有任何选项可以通过使用意图来停止活动。

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
startActivity(intent);

这是我的代码。如果用户忙或其他情况,我想停止此活动(也就是说,我想挂断此电话)。我能为此做些什么?我试过这个:

if (condition) {
    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
    startActivity(intent);
}else {
    this.finish();
}

但是没有用。有人有建议吗?

【问题讨论】:

  • 如果一个应用程序可以杀死另一个应用程序,这似乎是一种安全违规。
  • 除非目标应用程序通过 Intent 发布此功能。

标签: android android-intent


【解决方案1】:

几天前我遇到了这个问题,我很高兴地告诉你我已经找到了解决这个问题的方法。

首先,在你想停止的活动中添加AndroidManifest.xml

android:launchMode="singleTop"

我将使用 CheckBox 示例。当它被选中时,活动开始,当取消选中时,将终止活动。

示例 Activity A 正在调用 Activity B,然后使用意图杀死它。

要放入A的代码:

checkbox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(A.this, B.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            if (enable.isChecked()) {
                intent.putExtra("keep", true);
                startActivity(intent);
            }
            else
            {
                intent.putExtra("keep", false);
                startActivity(intent);
            }
        }
    });

要放入B的代码:

boolean keep;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.B);
    intent = this.getIntent();
    boolean keep = intent.getExtras().getBoolean("keep");
    if(keep==true)
    {
        //execute your code here

    }
 }
    @Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    keep = intent.getExtras().getBoolean("keep");
    if(keep==false)
    {
        B.this.finish();
    }
}

解释:这基本上做的是,当复选框被选中时,它调用活动并传递一个布尔值,如果它是真的,活动保持活动状态并被带到前台。现在,如果您不传递标志singleTop,那么将创建此活动的许多实例。 singleTop 确保只调用同一个实例。现在,当未选中该复选框时,将传递一个新的 keep 值,该值在 B 中进行验证。如果未选中,则 Activity A 将传递 false,因此 B 从 onNewIntent() 函数中终止自身。

P.S - 您也可以从另一个 Activity 关闭 Activity B。只需使用 如果其他活动是C:

Intent intent = new Intent(C.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("keep", false);
startActivity(intent);

【讨论】:

  • 非常感谢 Torcellite!这可能对我有很大帮助。
  • @Tony 任何时候,请投票给其他人查看,以防他们遇到这种情况:)
  • @Tony 另外,尝试一下 B 中的代码,除非我不知道您到底想要什么,否则它可能不会很好地工作。
  • 不!我从第一个本身(代码 A)中得到了这个想法。实际上,我想通过使用意图拨打电话,并且如果用户正忙于另一个电话,则必须切断通话。这就是为什么我使用 StartActivity(Intent);但不知道如何阻止它。我搜索了很多类似 stopActivity 的方法。但是还没找到。
  • @KarthikBalakrishnan 我投了赞成票,因为它让我走上了正确的道路。但是,我不需要提到的清单条目,而是使用android:excludeFromRecents="true" 并在onStart() 中处理额外的布尔值keep,而不使用onNewIntent()。然后我使用startActivity(intent) 并设置这些标志:FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_SINGLE_TOP
【解决方案2】:

你可以关闭play store的后台数据和play services不进入play store。它会简单的说, “无法加载” 这是我发现停止意图的唯一方法

【讨论】:

  • @Sree 链接在哪里?
【解决方案3】:
This worked for me:

AndroidManifest.xml:

        <activity android:name=".ui.activities.SettingsActivity"
            android:launchMode="singleTop"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.STOP_SERVICE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>

意图:

Intent stopBackgroundServiceIntent = new Intent(this, SettingsActivity.class);
// Remove the activity when finish is called
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK );
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多