Android Intent.ACTION_VIEW 基本身份验证

Android Intent.ACTION_VIEW Basic Authentication

如何将 HTTP 基本身份验证信息传递给 Intent.ACTION_VIEW?这是我发射意图的地方:

public class OutageListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    // ...

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        // Get a URI for the selected item, then start an Activity that displays the URI. Any
        // Activity that filters for ACTION_VIEW and a URI can accept this. In most cases, this will
        // be a browser.
        String outageUrlString = "http://demo:demo@demo.opennms.org/opennms/outage/detail.htm?id=204042";
        Log.i(TAG, "Opening URL: " + outageUrlString);
        // Get a Uri object for the URL string
        Uri outageURI = Uri.parse(outageUrlString);
        Intent i = new Intent(Intent.ACTION_VIEW, outageURI);
        startActivity(i)
    }

}

我也试过Uri.fromParts(),同样的交易。卷曲效果很好。

事实证明,您可以通过 Bundle 添加 HTTP headers 到 Intent,并专门添加一个具有 Base64 编码用户 ID 的授权 header。

    Intent i = new Intent(Intent.ACTION_VIEW, outageURI);

    String authorization = user + ":" + password;
    String authorizationBase64 = Base64.encodeToString(authorization.getBytes(), 0);

    Bundle bundle = new Bundle();
    bundle.putString("Authorization", "Basic " + authorizationBase64);
    i.putExtra(Browser.EXTRA_HEADERS, bundle);
    Log.d(TAG, "intent:" + i.toString());

    startActivity(i);