【问题标题】:how to open content shared by other apps?如何打开其他应用共享的内容?
【发布时间】:2016-08-29 01:56:10
【问题描述】:

我想在我的 web 视图中打开由 Firefox 或 youtube 等应用共享的链接。每当我按下分享按钮时,it launches my app,但它从不加载 url-

// get URL from browsable intent filter
    TextView uri = (TextView) findViewById(R.id.urlField);
    // get the url
    url = getIntent().getStringExtra(Intent.EXTRA_TEXT);
    Uri data = getIntent().getData();
    if (data == null) {
        uri.setText("");
    } else {
        uri.setText(getIntent().getData().toString());
        fadeout();
    }
    // }

    webView = (WebView) findViewById(R.id.webView);
    // Load URL from Browsable intent filter if there is a valid URL pasted
    if (uri.length() > 0)
        webView.loadUrl(url);
    else
        // dont load

清单

 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
            <data android:mimeType="image/*" />
        </intent-filter>

【问题讨论】:

    标签: android


    【解决方案1】:

    对不起,我误解了你的问题。我以为您正在尝试打开由另一个应用程序共享的文本链接。顺便说一句,我已尝试重现此问题,这是我的解决方案。

    清单:

    <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    

    这是我的活动

    public class MainActivity extends AppCompatActivity {
    
    private WebView webView;
    private String url;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        initView();
    
        Intent intent = getIntent();
        String action = intent.getAction();
    
        if (Intent.ACTION_SEND.equals(action)) {
            url = getIntent().getStringExtra(Intent.EXTRA_TEXT);
        }
    }
    
    private void initView(){
        webView = (WebView)findViewById(R.id.webView);
    
        //add this to enable video streaming (youtube etc.) 
        //or to handle webpage with javascript
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setJavaScriptEnabled(true);
    }
    
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    
        if(null != url) {
            Log.i("URL", url);
            webView.loadUrl(url);
        }
    }
    

    如果它不起作用,请告诉我。

    【讨论】:

    • 在您的情况下,只需将 url = getIntent().getDataString() 更改为 url = getIntent().getStringExtra(Intent.EXTRA_TEXT)
    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多