WebView ক্লিক আচরণ অপ্টিমাইজ করুন

যদি আপনার Android অ্যাপটি ওয়েব কন্টেন্ট প্রদর্শনের জন্যWebView ব্যবহার করে, তাহলে আপনি নিম্নলিখিত কারণে ক্লিক আচরণ অপ্টিমাইজ করার কথা বিবেচনা করতে চাইতে পারেন:

  • WebView কাস্টম ইউআরএল স্কিম সমর্থন করে না যা বিজ্ঞাপনে ফেরত দেওয়া যেতে পারে যদি ক্লিক গন্তব্য একটি পৃথক অ্যাপে হয়। উদাহরণস্বরূপ, একটি Google Play ক্লিক-থ্রু URL ব্যবহার করতে পারে market://

এই নির্দেশিকাটি ওয়েব ভিউ বিষয়বস্তু সংরক্ষণ করার সময় মোবাইল ওয়েব ভিউতে ক্লিক আচরণ অপ্টিমাইজ করার জন্য প্রস্তাবিত পদক্ষেপগুলি প্রদান করে৷

পূর্বশর্ত

বাস্তবায়ন

আপনারWebView উদাহরণে ক্লিক আচরণ অপ্টিমাইজ করতে এই পদক্ষেপগুলি অনুসরণ করুন:

  1. Override shouldOverrideUrlLoading() WebViewClient এ। এই পদ্ধতিটি বলা হয় যখন একটি URL বর্তমান WebView এ লোড হতে চলেছে।

  2. ক্লিক URL-এর আচরণ ওভাররাইড করবেন কিনা তা নির্ধারণ করুন।

    নীচের কোড স্নিপেট বর্তমান ডোমেন লক্ষ্য ডোমেন থেকে ভিন্ন কিনা তা পরীক্ষা করে। এটি শুধুমাত্র একটি পদ্ধতি কারণ আপনি যে মানদণ্ড ব্যবহার করেন তা পরিবর্তিত হতে পারে।

  3. একটি বাহ্যিক ব্রাউজারে, Android কাস্টম ট্যাবগুলিতে বা বিদ্যমান ওয়েব ভিউতে URL খুলবেন কিনা তা স্থির করুন৷ এই নির্দেশিকাটি দেখায় যে কীভাবে অ্যান্ড্রয়েড কাস্টম ট্যাবগুলি চালু করে সাইট থেকে দূরে সরে গিয়ে URLগুলি খুলতে হয়৷

কোড উদাহরণ

প্রথমে আপনার মডিউল-স্তরের build.gradle ফাইলে androidx.browser নির্ভরতা যোগ করুন, সাধারণত app/build.gradle । এটি কাস্টম ট্যাবের জন্য প্রয়োজন:

dependencies {
  implementation 'androidx.browser:browser:1.5.0'
}

নিম্নলিখিত কোড স্নিপেট দেখায় কিভাবে shouldOverrideUrlLoading() বাস্তবায়ন করতে হয়:

জাভা

public class MainActivity extends AppCompatActivity {

  private WebView webView;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ... Register the WebView.

    webView = new WebView(this);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient(
        new WebViewClient() {
          // 1. Implement the web view click handler.
          @Override
          public boolean shouldOverrideUrlLoading(
              WebView view,
              WebResourceRequest request) {
            // 2. Determine whether to override the behavior of the URL.
            // If the target URL has no host, return early.
            if (request.getUrl().getHost() == null) {
              return false;
            }

            // Handle custom URL schemes such as market:// by attempting to
            // launch the corresponding application in a new intent.
            if (!request.getUrl().getScheme().equals("http")
                && !request.getUrl().getScheme().equals("https")) {
              Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
              // If the URL cannot be opened, return early.
              try {
                MainActivity.this.startActivity(intent);
              } catch (ActivityNotFoundException exception) {
                Log.d("TAG", "Failed to load URL with scheme:" + request.getUrl().getScheme());
              }
              return true;
            }

            String currentDomain;
            // If the current URL's host cannot be found, return early.
            try {
              currentDomain = new URL(view.getUrl()).getHost();
            } catch (MalformedURLException exception) {
              // Malformed URL.
              return false;
            }
            String targetDomain = request.getUrl().getHost();

            // If the current domain equals the target domain, the
            // assumption is the user is not navigating away from
            // the site. Reload the URL within the existing web view.
            if (currentDomain.equals(targetDomain)) {
              return false;
            }

            // 3. User is navigating away from the site, open the URL in
            // Custom Tabs to preserve the state of the web view.
            CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
            intent.launchUrl(MainActivity.this, request.getUrl());
            return true;
          }
        });
  }
}

কোটলিন

class MainActivity : AppCompatActivity() {

  private lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // ... Register the WebView.

    webView.webViewClient = object : WebViewClient() {
      // 1. Implement the web view click handler.
      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {
        // 2. Determine whether to override the behavior of the URL.
        // If the target URL has no host, return early.
        request?.url?.host?.let { targetDomain ->
          val currentDomain = URL(view?.url).host

          // Handle custom URL schemes such as market:// by attempting to
          // launch the corresponding application in a new intent.
          if (!request.url.scheme.equals("http") &&
              !request.url.scheme.equals("https")) {
            val intent = Intent(Intent.ACTION_VIEW, request.url)
            // If the URL cannot be opened, return early.
            try {
              this@MainActivity.startActivity(intent)
            } catch (exception: ActivityNotFoundException) {
              Log.d("TAG", "Failed to load URL with scheme: ${request.url.scheme}")
            }
            return true
          }

          // If the current domain equals the target domain, the
          // assumption is the user is not navigating away from
          // the site. Reload the URL within the existing web view.
          if (currentDomain.equals(targetDomain)) {
            return false
          }

          // 3. User is navigating away from the site, open the URL in
          // Custom Tabs to preserve the state of the web view.
          val customTabsIntent = CustomTabsIntent.Builder().build()
          customTabsIntent.launchUrl(this@MainActivity, request.url)
          return true
        }
        return false
      }
    }
  }
}

আপনার পৃষ্ঠা নেভিগেশন পরীক্ষা করুন

আপনার পৃষ্ঠা নেভিগেশন পরিবর্তন পরীক্ষা করতে, লোড

https://webview-api-for-ads-test.glitch.me#click-behavior-tests

আপনার ওয়েব ভিউতে। আপনার অ্যাপে তারা কীভাবে আচরণ করে তা দেখতে বিভিন্ন ধরনের লিঙ্কের প্রতিটিতে ক্লিক করুন।

এখানে চেক করার জন্য কিছু জিনিস আছে:

  • প্রতিটি লিঙ্ক উদ্দিষ্ট URL খোলে।
  • অ্যাপে ফিরে আসার সময়, পৃষ্ঠার অবস্থা সংরক্ষিত ছিল তা যাচাই করতে পরীক্ষার পৃষ্ঠার কাউন্টার শূন্যে রিসেট করে না।