java.lang.IllegalArgumentExceptionが:によって引き起こされるサービスの意図は、明示的である必要があります:テント{行為= com.aid

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/Maiduoudo/article/details/97933647

明示的なクラッシュでなければならないサービスの意図を解決

Caused by: java.lang.IllegalArgumentException:
Service Intent must be explicit: Intent { act=com.aidl.server.myserver }



アプリ間で通信する機能を記述するとき今日、いくつかの小さな問題は、クライアント側のアプリケーションが起動している間にクラッシュで、その結果、サービスのAIDL終わりを結合を介してクライアントサービスが行われている、次のように書かれた初期のコード:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    Intent intent = new Intent("com.aidl.server.myserver");
    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}



エラーログは次のよう:

Service Intent must be explicit: Intent { act=com.aidl.server.myserver }



ソースコードを見てで、次の判断ContextImplが見つかりました:

@Override
public boolean bindService(Intent service, ServiceConnection conn,
            int flags) {
    warnIfCallingFromSystemProcess();
    return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
            Process.myUserHandle());
}
private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
            handler, UserHandle user) {
    ...
    validateServiceIntent(service);
    ...
}

private void validateServiceIntent(Intent service) {
    if (service.getComponent() == null && service.getPackage() == null) {
        if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
            IllegalArgumentException ex = new IllegalArgumentException(
                        "Service Intent must be explicit: " + service);
            throw ex;
        } else {
            Log.w(TAG, "Implicit intents with startService are not safe: " + service
                    + " " + Debug.getCallers(2, 3));
        }
    }
}



このように、意図をアクションを設定することによって得られるのでAndroid5.0の意図の増加の判定は、何コンポーネントオブジェクトインスタンスが存在しない、何のパッケージ名、したがって誤りがありません。理由が発見され、パッケージ名を設定するためのステップを追加し、次のようにする必要は、アプリのパッケージ名、パッケージ名ではなく、パッケージがスムーズに解決することができるサービスクラスです。

        Intent intent = new Intent("com.aidl.server.myserver");
        intent.setPackage("com.aidl.server.myserver");
        bindService(intent, conn, Context.BIND_AUTO_CREATE);


 

おすすめ

転載: blog.csdn.net/Maiduoudo/article/details/97933647