2017-07-29 4 views
1

私は少し混乱していますインテントです。その定義で何か新しいものを追加していないようですしながら、明示的かつ暗示的な意図

Intent explicit=new Intent(implicit); 

明示的な意図ありながら

なぜ

Intent implicit=new Intent(IDownload.class.getName()); 

暗黙の意思

です。上記のimplicitの意図によってこれまでに提供されていなかった新しい情報は、システムから引き出されていないようです。 Android documentation (Intent types)

明示的なインテント名(完全修飾クラス名)によって開始するコンポーネントを指定します。あなたが開始する活動やサービスのクラス名を知っているので、通常は

Intent implicit=new Intent(IDownload.class.getName());

は、この要件を満たしているようだ......、独自のアプリでコンポーネントを起動するために、明示的な意図を使用します

暗黙インテントの他のアプリからのコンポーネントがそれを処理することを可能にする、特定のコンポーネントに名前を付けるが、代わりに実行するために、一般的なアクションを宣言していない:、まだドキュメントあたりとしては、暗黙の意思、とみなされています.....

Intent explicit=new Intent(implicit);はこれと矛盾するようですが、依然として明示的な意図とみなされます。

UPDATE - サンプル実装

REF:Binding to a Service

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setRetainInstance(true); 

    appContext=(Application)getActivity().getApplicationContext(); 

    Intent implicit=new Intent(IDownload.class.getName()); 
    List<ResolveInfo> matches=getActivity().getPackageManager() 
    .queryIntentServices(implicit, 0); 

    if (matches.size() == 0) { 
    Toast.makeText(getActivity(), "Cannot find a matching service!", 
     Toast.LENGTH_LONG).show(); 
    } 
    else if (matches.size() > 1) { 
    Toast.makeText(getActivity(), "Found multiple matching services!", 
     Toast.LENGTH_LONG).show(); 
    } 
    else { 
    ServiceInfo svcInfo=matches.get(0).serviceInfo; 

    try { 
     String otherHash=SignatureUtils.getSignatureHash(getActivity(), 
     svcInfo.applicationInfo.packageName); 
     String expected=getActivity().getString(R.string.expected_sig_hash); 

     if (expected.equals(otherHash)) { 
     Intent explicit=new Intent(implicit); 
     ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName, 
      svcInfo.name); 

     explicit.setComponent(cn); 
     appContext.bindService(explicit, this, Context.BIND_AUTO_CREATE); 
     } 
     else { 
     Toast.makeText(getActivity(), "Unexpected signature found!", 
      Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch (Exception e) { 
     Log.e(getClass().getSimpleName(), "Exception trying to get signature hash", e); 
    } 
    } 
} 
+0

'Intent explicit =新しいIntent(暗黙的);'は明示的な意図ですか? –

+0

https://stackoverflow.com/a/37907805/3663765 @BenP。 –

+0

この例では、Intentは 'explicit.setComponent(cn)'を呼び出すまでは暗黙のインテントです。インテントを明示的にするのはコンポーネント名の設定です。彼は変数 '明示的に'を命名しているに過ぎません。 –

答えて

1

リンクされている例が含まれ、これらのライン

 Intent explicit=new Intent(implicit); 
     ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName, 
     svcInfo.name); 

     explicit.setComponent(cn); 

最初の行は、単にのコピーだ新しいIntentインスタンスを作成します古いもの。変数はexplicitですが、この時点ではまだ暗黙のインテントです。

2行目は、以前のgetActivity().getPackageManager().queryIntentServices(implicit, 0)コールの1つの一致結果に基づいて、ComponentNameオブジェクトを作成します。この時点では、explicitは依然として暗黙のインテントです。

3行目は魔法です。特定のComponentNameIntentインスタンスに割り当てられると、explicitは本当に明示的な意図になります。

+0

'IDownload.class.getName()'は、これを完全修飾クラス名 - [Androidドキュメント(インテントタイプ)](https://developer.android.com/guide/components/intents-filters)として修飾しません。 html#Types)? –

+1

'Class.getName()'は 'String'を返します。つまり' Intent(String) 'コンストラクタを使用しています。この場合、文字列は「アクション」です。**は「完全修飾クラス名」ではありません。 'Intent(Context、Class)'や 'Intent(String、Uri、Context、Class)'コンストラクタを使う必要があります。 –

関連する問題