Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

disable the intent filter from android manifest programmatically

In my activity I have a web view and in manifest.xml I have declared intent filter like this

 <activity
        android:name=".ui.socialNetwork.MySocialNetworkActivity"
        android:configChanges="orientation|screenSize"
        android:process=":fb"
        android:screenOrientation="portrait" >

    </activity>

    <activity-alias
        android:targetActivity=".ui.socialNetwork.MySocialNetworkActivity"
        android:name=".AliasMySocialNetworkActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.PROCESS_TEXT" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity-alias>

This is not launcher activity. The intent filter used here is for copy paste toolbar on web view long press. This works fine. In addition to this I want to use Webview.setOnLongClickListener() for additional options, and I implemented like this.

webView = (WebView) findViewById(R.id.webview);

PackageManager pm = getApplicationContext().getPackageManager();
    ComponentName compName =
            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
    pm.setComponentEnabledSetting(
            compName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);

    webView.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            WebView.HitTestResult hitResult = null;
            hitResult = webView.getHitTestResult();
            if (hitResult != null && hitResult.getExtra() != null) {
                final String hitRes = hitResult.getExtra();
                if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE || hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    Intent ImageSaveIntent = new Intent(getApplicationContext(), SaveImage.class);
                    ImageSaveIntent.putExtra("putImage", hitRes);
                    startActivity(ImageSaveIntent);
                }
                if (hitResult.getType() != WebView.HitTestResult.IMAGE_TYPE || hitResult.getType() != WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    PackageManager pm = getApplicationContext().getPackageManager();
                    ComponentName compName =
                            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
                    pm.setComponentEnabledSetting(
                            compName,
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);
                                       }
            }
            return true;
        }
    });

My problem is that

  1. If I use intent filter alone without webview.setOnLongClickListener(), I can copy paste the text in webview

  2. If I use webview.setOnLongClickListener() alone, I can do my additional options and it is working fine.

  3. If I implement both intent filter and webview.setOnLongClickListener(), I cannot copy paste the text from webview. webview.setOnLongClickListener() will work fine. Here I understood that both functionalities depends on longPress, But I want both to work together.

I searched Webview.HitResult options for TextType, but it is not having such option. https://developer.android.com/reference/android/webkit/WebView.HitTestResult.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this (disable the intent filter from android manifest programatically) by using Activity Alias:

1) add (e.g. AliasMySocialNetworkActivity) in AndroidManifest.xml to your MySocialNetworkActivity and move your intent-filter to them. It will be looks like that:

         <activity-alias
            android:targetActivity=".MySocialNetworkActivity"
            android:name=".AliasMySocialNetworkActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.PROCESS_TEXT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
             </intent-filter>
        </activity-alias>

2) add this code to suppress intent-filter in alias activity when You need that

PackageManager pm = getApplicationContext().getPackageManager();
        ComponentName compName =
                new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
        pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

3) restore intent-filter, when You need it:

PackageManager pm = getApplicationContext().getPackageManager();
    ComponentName compName =
            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
    pm.setComponentEnabledSetting(
            compName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

For more details see this or that answer.

Update

Actually You don't need Alias, just use PackageManager.COMPONENT_ENABLED_STATE_DISABLED/PackageManager.COMPONENT_ENABLED_STATE_ENABLED

PackageManager pm = getApplicationContext().getPackageManager();
        ComponentName compName =
                new ComponentName(getPackageName(), getPackageName() + ".MySocialNetworkActivity");
        pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

directly on your activity. Thanks @pskink.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...