programing

Android 암시 적 인 텐트 VS 명시 적 인 텐트

nasanasas 2020. 12. 6. 21:37
반응형

Android 암시 적 인 텐트 VS 명시 적 인 텐트


Android로 작업하면서 암시 적 의도는 유연성으로 인해 대부분의 경우에 좋은 선택이라는 것을 깨달았습니다. 그러나 명시 적 의도는 무엇입니까? 그것들을 사용하면 어떤 이점이 있습니까? 그것들을 사용하는 것이 좋은 습관 인 일반적인 경우는 무엇입니까?


암시 적 인 텐트는 호출해야하는 Android 구성 요소를 직접 지정하지 않고 수행 할 작업 만 지정합니다. Uri를 암시 적 의도와 함께 사용하여 데이터 유형을 지정할 수 있습니다.

예를 들면

Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com"));

이렇게하면 웹 브라우저에서 웹 페이지가 열립니다. 안드로이드 시스템은 특정 동작과 데이터 타입에 등록 된 모든 컴포넌트를 검색하며, 많은 컴포넌트가 발견되면 사용자가 사용할 컴포넌트를 선택할 수 있습니다.

한 활동이 다른 활동으로 전환 될 수있는 애플리케이션 자체에서 명시 적 의도가 사용됩니다 ... 예 Intent intent = new Intent(this,Target.class);를 들어 현재 컨텍스트에서 대상 활동으로 활동이 전환됩니다. 명시 적 인 텐트는 putExtra메소드를 사용하여 다른 활동에 데이터를 전달하는 데 사용될 수도 있으며 메소드에 의해 대상 활동에 의해 검색됩니다 getIntent().getExtras().

이것이 도움이 되었기를 바랍니다.


일반적으로 자체 애플리케이션 내에서 활동을 시작하기 위해 명시 적 인 텐트를 사용합니다. 이 시점에서 시작하려는 활동을 정확히 알 수 있으므로 암시 적 의도를 설정하는 추가 작업을 수행 할 이유가 없습니다.


  1. 명시 적 인 텐트는 특정 구성 요소를 호출하는 데 사용됩니다. 실행할 구성 요소를 알고 있고 사용할 구성 요소를 사용자에게 자유롭게 제어하지 않으려는 경우 예를 들어 2 개의 활동이있는 애플리케이션이 있습니다. 활동 A 및 활동 B. 활동 A에서 활동 B를 시작하려고합니다.이 경우 명시 적 인 텐트 타겟팅 activityB를 정의한 다음이를 사용하여 직접 호출합니다.

  2. 암시 적 인 텐트는 수행 할 작업에 대한 아이디어가 있지만 어떤 구성 요소를 시작해야하는지 모르는 경우에 사용됩니다. 또는 사용할 구성 요소 목록 중에서 선택할 수있는 옵션을 사용자에게 제공하려는 경우. 이러한 인 텐트가 Android 시스템에 전송되면 특정 작업 및 데이터 유형에 대해 등록 된 모든 구성 요소를 검색합니다. 구성 요소가 하나만 발견되면 Android는 구성 요소를 직접 시작합니다. 예를 들어 카메라를 사용하여 사진을 찍는 애플리케이션이 있습니다. 응용 프로그램의 기능 중 하나는 사용자가 찍은 사진을 보낼 수있는 가능성을 제공하는 것입니다. 사용자가 사진을 보낼 수있는 응용 프로그램의 종류를 알 수 없으며 사용자에게 둘 이상의 외부 응용 프로그램이있는 경우 사용할 외부 응용 프로그램을 선택할 수있는 옵션을 제공하려고합니다. 이 경우 명시적인 의도를 사용하지 않습니다.

    명시 적 의도는 포함 된 내용에 관계없이 항상 대상에 전달됩니다. 필터는 참조되지 않습니다. 그러나 암시 적 의도는 구성 요소의 필터 중 하나를 통과 할 수있는 경우에만 구성 요소에 전달됩니다.


1) 명시 적 의도 : 개발자가 알고있는 컴포넌트 이름, 의도에 지정된 이름.

2) 암시 적 의도 : 의도에서 구성 요소를 지정하지 않았습니다.


KEY : 알고있을 때와 모를 때

명시 적 의도 :

Activity요청을 처리 할 수있는 것을 정확히 알고있는 경우 명시적인 의도를 사용하십시오 .

예 : 목록 활동이 있고 목록에서 항목을 클릭하면 세부 사항 활동이 열립니다. 이 경우 응용 프로그램 에서 항목의 세부 정보를 표시하거나 처리 할 수 ​​있음 알고 있습니다 DetailActivity.class. 따라서이 작업을 수행하려면 클래스 이름을 명시 적으로 지정하여 인 텐트를 만듭니다.

Intent showDeatil = new Intent(this,DetaiActivy.class);  
startActivity(showDeatil);

암시 적 의도 :

요청을 처리 할 수있는 애플리케이션의 활동을 모르는 경우 암시 적 인 텐트를 사용하십시오.

예 : 링크가 있습니다. 링크를 클릭하면 일부 브라우저에서 웹 페이지가 열립니다. 당신은 모르겠다 되는 응용 프로그램에서 요청을 처리 할 수있는 활동을 정확하게. 웹 페이지 링크라는 모호한 생각이 있으므로 누군가가 열릴 때 일부 브라우저에서 웹 페이지를 열어야합니다. 이 경우 ACTION을 지정하기 만하면 OS가 나머지를 처리합니다.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

보너스:

OS는 어떻게 결정합니까?

그것에 대한 용어가 있습니다. 이를 의도 해결이라고합니다.
의도 해결에서.

  • OS는 의도에 지정된 ACTION을 수행합니다.

  • PackageManager로 이동하여 장치에 설치된 모든 응용 프로그램과 일치하는 ACTION으로 등록 된 모든 활동을 찾습니다.

  • 일치하는 모든 응용 프로그램의 목록을 팝업으로 표시합니다.

암시 적 인 텐트를 작성하는 더 안전한 방법입니다.

때때로 ACTION과 일치하는 활동이 없을 수도 있습니다. 이 경우 NullPointerException이 발생합니다. 그래서 더 선호되는 방법은

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

응용 프로그램이 해당 팝업 목록에 들어가도록하는 방법은 무엇입니까?

브라우저 애플리케이션을 작성했다고 가정 해 보겠습니다. 누군가가 링크를 열 때 팝업 목록에 응용 프로그램을 표시하려는 경우. 그런 다음 Intent Filters AndroidManifest.xml 파일을 사용하여 활동에 활동을 등록해야합니다. 이렇게.

<application
    .....  >

    ......
    <activity android:name=".YourBrowserActivity">
        <action android:name="android.intent.action.VIEW" />       
        <data android:scheme="http" android:host="www.example.com" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
    </activity>
 ..... 


</application>

References
Common Intent ACTIONS and their Intent-Filters list
More on Intent filters and Intent resolution


From Docs:

There are two types of intents:

  • Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, you can start a new activity in response to a user action or start a service to download a file in the background.
  • Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

  1. Implicit intent - When we want to call the system components through intent to perform a particular task and we don't really know the name of the components to be used, the android system will show the desired list of applications to perform the task.
  2. Explicit intent - When we want to call the another activity with the full qualified name of the activity and of course we know the name of the activity.

Simply we can describe both intents like this..

Explicit Intents : They are used for communication between two activities in a single application.

eg : Consider an application which has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page which displays the username field which we entered before.In this case we use explicit intents because we need to change the activities and to carry data from one activity to the other activity(username field) in the same application.

Implicit Intents : They are used for communication between two activities of different applications.

eg : consider a news app which describes about an accident in which the video of accident is recorded and uploaded in Facebook. While clicking on the link given in the news app it will direct us to Facebook .In this case the communication is between an activity in news app and and an activity in Facebook app.For this purpose we use Implicit Intents.

I hope you can understand.


Implicit Intent

  • It Pulls up the new Application without being specified which one to pull up.
  • It specifies only action to be performed and do not directly specify Android Components.
  • URI can be used with implicit Intent to specify the type of data.

Explicit Intent

  • It will pull up a specific application and is set while writing the code.
  • It is used in application itself wherein one activity can switch to other activity.
  • Used to pass data to other activity using put extra method and retrieved by target activity getIntent().
  • It is always delivered to target even filter is not consulted.

참고URL : https://stackoverflow.com/questions/2914881/android-implicit-intents-vs-explicit-intents

반응형