【问题标题】:How to "disable" external applications' intent-filters?如何“禁用”外部应用程序的意图过滤器?
【发布时间】:2013-06-11 21:14:34
【问题描述】:

我正在编写一个使用 od NFC 标签的应用程序。我希望 NFC 标签成为我的应用程序的入口点 - 关闭包含标签的卡片到电话应该开始我的活动。然后,当活动运行时,我想禁用所有可以使用 NFC 标签的意图过滤器,所以另一个特写镜头不会做任何事情(我不希望我的活动重新开始)。我知道如何使用活动别名“禁用”我的意图过滤器:

    <activity-alias android:enabled="true"
            android:name=".alias.NFCEntryPoint"
            android:targetActivity="pl.mitrue.safedb.NFCEntryPoint"
            android:exported="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/alias.pl.mitrue.safedb.NFCEntryPoint" >
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

然后我可以以编程方式禁用别名,因此我的意图过滤器也被禁用:

getPackageManager().setComponentEnabledSetting(new ComponentName("pl.mitrue.safedb", "pl.mitrue.safedb.alias.NFCEntryPoint"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

现在的问题是:我的设备上安装了使用这种过滤器的其他应用程序。现在,当我的应用程序正在运行并且我禁用了我的意图过滤器(通过禁用活动别名)时,会出现一个对话框,要求我选择要使用的应用程序。有没有办法避免这种情况?在最简单的情况下,一旦我的应用程序启动,我不想在关闭 NFC 标签时采取任何行动。有什么方法可以让我的应用程序成为唯一可以接收外部意图的应用程序?

郑重声明:我不想完全关闭 NFC(我什至不知道是否可能),因为我以后可能想在其他活动中使用它。

我希望我已经表达清楚了。请理解,因为我是新来的。

谢谢!

【问题讨论】:

  • 不,你不能。您正在尝试覆盖系统安全性,因为您正在运行沙盒,因此您无法控制此行为。
  • 好的,但不知何故,我安装的其中一个应用程序具有所需的行为。在应用程序的某些地方关闭标签什么都不做,在其他地方它执行一个动作(并不总是相同)。在使用自己的应用程序时,我从来没有被要求选择像我这样的活动。这正是我需要的行为。也许我可以使用某种解决方法?我不知道是否可以获取和反编译从 Google Market 安装的应用程序的源代码 - 如果可以,如果我在这里没有得到答案,我会这样做。

标签: android android-intent


【解决方案1】:

我遇到了类似的问题,其中: 1)如果我在单元格的开始屏幕(所有应用程序都关闭),当传递一个mifare经典卡时,自动打开我的应用程序,如果有很多也读mifare经典的应用程序,不问要打开哪个应用程序。

2) 如果我的应用程序已打开并通过了卡片,请不要关闭我的应用程序并询问使用哪个应用程序,如果我有许多应用程序也读取 mifare classic。 解决方法:使用Foreground Dispatch

在我的主要活动中:

private IntentFilter[] intentFiltersArray;
private String[][] techListsArray;
private PendingIntent pendingIntent;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("text/plain");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        intentFiltersArray = new IntentFilter[] {ndef, };
        techListsArray = new String[][] { new String[] { android.nfc.tech.MifareClassic.class.getName() } };
    }

@Override
    protected void onResume() {
    super.onResume();
    adaptadorNFC = NfcAdapter.getDefaultAdapter(this);
    System.out.println("Se setearan parametros");
    final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    adaptadorNFC.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

    leerTarjeta(this.getIntent());
}

@Override
protected void onPause() {
    System.out.println("Se dessetearan parametros");
    adaptadorNFC.disableForegroundDispatch(this);
    super.onPause();
}

希望这对其他人有所帮助,因为这个问题是在一年前提出的:P

【讨论】:

  • 这并不能真正回答问题。如果您有其他问题,可以点击 提问。一旦你有足够的reputation,你也可以add a bounty引起更多的关注。
  • 确实如此。可能不完全,但我认为 @mitrue 是应该确定这一点的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 2021-05-31
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多