问题描述:

CodeImageI have a Android Webview on my emulator with a button.Based on the button click or hyperlink I need to open an existing app on the emulator for example Calendar.How should I go about it?

网友答案:
  1. To do this your WebView must have your custom WebViewClient
  2. Your Android WebView HTML content must have specific hyperlinks.

    Activity should be like

    package com.hrupin.sample.webviewintent;
    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class SendIntentByHyperlinkClickActivity extends Activity {
    
    private WebView webView1;
    
    
    
    /** Called when the activity is first created. */
    
    @Override
    
    public void onCreate(Bundle savedInstanceState) {
    
     super.onCreate(savedInstanceState);
    
     setContentView(R.layout.main);
    
     webView1 = (WebView) findViewById(R.id.webView1);
    
     String summary = "<html><head><title>Title of the document</title></head><body><h1><a href=\"hrupin://second_activity\">LINK to second activity</a></h1><h1><a href=\"http://www.google.com/\">Link to GOOGLE.COM</a></h1></body></html>";
    
     webView1.loadData(summary, "text/html", null);
    
     webView1.setWebViewClient(new MyWebViewClient(this));
    
      }}
    

MyWebViewClient:

Attention in shouldOverrideUrlLoading method.

    package com.hrupin.sample.webviewintent;

    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MyWebViewClient extends WebViewClient {

    private Context context;

    public MyWebViewClient(Context context) {
    this.context = context;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.equals("hrupin://second_activity")){

        Intent i = new Intent(); 
       //for froyo and greater 
        cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity"); 

    //for less than froyo 
     cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");


        i.setComponent(cn);
        startActivity(i);
        return true;
    }
    return super.shouldOverrideUrlLoading(view, url);
    }
    }

If in case you do not know, you can call other applications following this

Its for calendar but others can be done following same.

相关阅读:
Top