google-developer-training / android-fundamentals-apps-v2

Other
649 stars 2.63k forks source link

Android fundamentals 02.3: Implicit intents #319

Closed thpir closed 2 years ago

thpir commented 2 years ago

When testing app on Samsung A50, Logging returns "Can't handle this intent!" when testing the intent to open a link and location. My code is exactly the same as the solution code. The problem was solved by removing the If-Else statement and put the "startActivity(intent)" right under the parsing of the URI.

Solution:

public void openWebsite(View view) { // Get the URL text. String url = mWebsiteEditText.getText().toString();

    // Parse the URI and create the intent.
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    // Find an activity to hand the intent and start that activity
    startActivity(intent);

    /*
    // Find an activity to hand the intent and start that activity
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent)
    } else {
        Log.d("ImplicitIntents", "Can't handle this intent!");
    }*/
}

public void openLocation(View view) {
    // Get the string indicating a location. Input is not validated; it is passed to the location handler intact.
    String loc = mLocationEditText.getText().toString();

    // Parse the location and create the intent.
    Uri addressUri = Uri.parse("geo:0,0?q=" + loc);
    Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
    // Find an activity to handle the intent, and start that activity.
    startActivity(intent);

    /*
    // Find an activity to handle the intent, and start that activity.
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    } else {
        Log.d("ImplicitIntents", "Can't handle this intent!");
    }*/
}

In which lesson and step of the codelab can this issue be found? Lesson number + step number.

[4. Task 2: Implement the Open Website button] - step 4 [5. Task 3: Implement the Open Location button] - step 4

How to reproduce? What are the exact steps to reproduce the problem? Copy the solution code - run app on Samsung Galaxy A50 (I don't think the device type matters)

Versions

  1. What version of Android Studio are you using? Arctic fox 2020.3.1
  2. What API level are you targeting? - targetSdk 31
hhyeok1026 commented 2 years ago

Add the queries tag to the menifest file.

You can check the cause in the link below. https://stackoverflow.com/questions/62535856/intent-resolveactivity-returns-null-in-api-30

</manifest>
 ...
 <application>
 ...   
 </application>
    <queries>
        <!-- Browser -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http" />
        </intent>
    </queries>
</manifest>