2016-12-04 3 views
0

私はこの問題を解決するためにあらゆるところを調査しましたが、私はそれが自分のマニフェストファイルと関係していると確信しています。ログに、 contextService = IContextInterface.Stub.asInterface(service)に以下のコードを接続するためのコードの印刷をしようとすると、サービスオブジェクトがまだnullであることを示すAIDLでサービスにアプリケーションをバインドする際の問題

contextServiceConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      contextService = IContextInterface.Stub.asInterface(service); 
      Toast.makeText(getApplicationContext(), 
        "Context Service Connected", Toast.LENGTH_SHORT) 
        .show(); 
      Log.d("IApp", "Binding is done - Context Service connected"); 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      // TODO Auto-generated method stub 
      contextService = null; 
      Toast.makeText(getApplicationContext(), "Service Disconnected", 
        Toast.LENGTH_SHORT).show(); 
      Log.d("IRemote", "Binding - Location Service disconnected"); 
     } 
    }; 
    if (contextService == null) { 
     Intent contextIntent = new Intent(); 
     contextIntent.setPackage("com.example.the_f.myapplication"); 
     contextIntent.setAction("service.contextFinder"); 
     bindService(contextIntent, contextServiceConnection, Context.BIND_AUTO_CREATE); 
     mContextIsBound = true; 
     if(contextService==null) 
      Log.d("locService","NULL"); 
    } 

その場合はnull、それはしようとしますように、私も、それをもう一度確認してください手動でバインドします。しかし、ログが "locService:NULL"を出力しているので、最後のif文にはまだヒットしています。ここで

私のクライアントアプリケーションマニフェストです:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

、ここでは私のリモートサービスマニフェストです:

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".MyMiddleware" 
     android:enabled="true" 
     android:exported="true"></service> 
</application> 

私はそれが間違って構築されたか、どこかに埋もれ、いくつかのファイルに問題があった考え続けているため、IVEは、このプログラムを3回再起動します。私は本当にアンドロイドプロではないので、多くのことをよく知っていません。どんな助けや指導もいただければ幸いです。ありがとうございます。

+0

あなたはonServiceConnectedを待たなければなりません。 – natario

+0

よろしくお願いします。私はprintステートメントをonServiceConnectedに落とし、そのステートメントを呼び出さなかった。私は何をしているのか分からない。私はかなり多くの例をオンラインで見たようにコピーしました。アンドロイドの開発サイトでさえ、彼らはこのように設定しています。だから明らかに私のアプリは決してサービスに接続していない –

答えて

0

あなたが投稿した記事から、欠落しているものが1つあります。あなたは意図

contextIntent.setAction("service.contextFinder"); 

上のアクションを設定している ただし、サービスマニフェストエントリ上で任意のフィルタを定義していません。

は、以下のことを試してみてください -

<service 
    android:name=".MyMiddleware" 
    android:enabled="true" 
    android:exported="true"> 
     <intent-filter> 
      <action android:name="service.contextFinder" /> 
     </intent-filter> 
</service> 

また、あなたは、必ずしもそれを必要とするが、あなたは自分のサービスに意図を制限したい場合は、パッケージがサービスアプリのものであることを確認していません。 natarioとして

contextIntent.setPackage("com.example.the_f.myapplication"); 

あなただけonServiceConnectedのほぼ間違いない権利bindService()呼び出しの後の前に()コールバック、ないし、スタブインスタンスを取得します、と述べました。

これが役に立ちます。

関連する問題