이 포스팅은 아래 구글 코드랩을 개인 학습용으로 정리한 글입니다.
To match your request with an app installed on the device
-> the Android system matches your implicit intent with an activity whose intent filters indicate that they can perform the action
If multiple apps match
-> the user is presented with an app chooser
-> lets them select which app they want to use to handle the intent.
String url = mWebsiteEditText.getText().toString();
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
}
class MainActivity : AppCompatActivity() {
lateinit var mWebsiteEditText : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mWebsiteEditText = findViewById(R.id.website_edittext)
}
fun openWebsite(view: View) {
val url = mWebsiteEditText.text.toString()
val webpage : Uri = Uri.parse(url)
val intent : Intent = Intent(Intent.ACTION_VIEW, webpage)
if(intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}else{
Log.d("Implicit Intents", "Can't handle this intent!")
}
}
}
String loc = mLocationEditText.getText().toString();
Uri addressUri = Uri.parse("geo:0,0?q=" + loc);
Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
fun openLocation(view: View) {
val loc = mLocationEditText.text.toString()
val adressUri = Uri.parse("geo:0,0?q=$loc")
val intent = Intent(Intent.ACTION_VIEW, adressUri)
if(intent.resolveActivity(packageManager) != null){
startActivity(intent)
}else{
Log.d("Implicit Intents", "Can't handle this intent!")
}
}
A share action:
an easy way for users to share items in your app with social networks and other apps.
Android provides the ShareCompat.IntentBuilder helper class
-> You can use ShareCompat.IntentBuilder to build an Intent and launch a chooser to let the user choose the destination app for sharing.
String txt = mShareTextEditText.getText().toString();
String mimeType = "text/plain";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle("Share this text with: ")
.setText(txt)
.startChooser();
from():
The Activity that launches this share Intent (this).
setType():
The MIME type of the item to be shared.
setChooserTitle():
The title that appears on the system app chooser.
setText():
The actual text to be shared
startChooser():
Show the system app chooser and send the Intent.
fun shareText(view: View) {
val txt : String = mShareEditText.text.toString()
val mimeType = "text/plain"
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle(R.string.share_text_with)
.setText(txt)
.startChooser()
}
➕ shareText()는 정상적으로 작동하지만, openWebsite()와 openLocation()는 암시적 인텐트를 실행시킬 수 있는 액티비티/앱을 찾지 못함
- 해결 방법:
- 매니페스트에 queries 요소를 추가
- intent.resolveActivity() 대신 packageManager.queryIntentActivities() 호출
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
fun openWebsite(view: View) {
val url = mWebsiteEditText.text.toString()
val webpage : Uri = Uri.parse(url)
val intent : Intent = Intent(Intent.ACTION_VIEW, webpage)
if(packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL) != null) {
startActivity(intent)
}else{
Log.d("Implicit Intents", "Can't handle this intent!")
}
}
fun openLocation(view: View) {
val loc = mLocationEditText.text.toString()
val adressUri = Uri.parse("geo:0,0?q=$loc")
val intent = Intent(Intent.ACTION_VIEW, adressUri)
if(packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL) != null){
startActivity(intent)
}else{
Log.d("Implicit Intents", "Can't handle this intent!")
}
}
- Android 11에서는 앱이 사용자가 기기에 설치한 다른 앱을 쿼리하고 상호작용하는 방법을 변경
- 요소를 사용하여 앱은 액세스할 수 있는 다른 패키지 집합을 정의
브라우저 또는 다른 앱에서 URL 열기
- 앱에서 URL을 열려고 하기 전에 기기에 사용 가능한 브라우저가 최소 하나 이상 있는지 또는 특정 브라우저가 기본 브라우저인지 확인하는 것이 좋습니다.
<!-- Place inside the <queries> element. --> <intent> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" /> </intent>
- queryIntentActivities()를 호출하고 웹 인텐트를 인수로 전달하면 경우에 따라 반환된 목록에 사용 가능한 브라우저 앱이 포함됩니다.