English 中文(简体)
网上通话和匿名广播
原标题:Trouble getting WebView and anonymous BroadcastReceiver communicating in an Activity
  • 时间:2012-01-11 17:10:45
  •  标签:
  • android

我想有一个匿名广播接收人对网上通话的链接作出答复。 我认为,我可以这样做,而事情却不奏效。 我的活动守则如下:

public class WebViewReceiverActivity extends Activity {

    private BroadcastReceiver mReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        webView.loadData("<a href="webview-receiver:///">This should Toast</a>", "text/html", "utf8");

        mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Uri data = intent.getData();
                if (data != null && data.getScheme().equals("webview-receiver")) {
                    Toast.makeText(WebViewReceiverActivity.this, "Toasty", Toast.LENGTH_SHORT).show();
                }
            }

        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_VIEW);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
        intentFilter.addDataScheme("webview-receiver");

        registerReceiver(mReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(mReceiver);
    }


}

这里的主要排位是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbarStyle="insideOverlay" />

</LinearLayout>

在点击网上电文中的链接时,我发现一页没有错误,没有发现任何图阿斯特。 这种做法是否可行? 我只想使用WebViewClient,而应当OverrideUrlLoading()的话,但如果我能找到广播接收人的办法,我更喜欢这样做。

最佳回答

我最后采取广播接收员和网络电文之间的混合做法:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Uri uri = Uri.parse(url);
        if (uri.getScheme().equals("webview-receiver")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);

            sendBroadcast(intent);

            return true;    
        }

        return false;
    }
});

我之所以这样做,是因为我正在根据现行法典进行工作。 简而言之,在“OverrideUrlLoading()”方法中采用“接收()”方法的广播接收器的所有逻辑,可能更为简单。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签