0

私は知っています、私は知っています、この質問はすでに数回尋ねられていますが、提案された解決策はここではうまくいかないようです.

私の問題は、onserviceConnected が呼び出されないことです。

情報: HelloWorld というサーバーを呼び出す電卓を開発しました。「追加」操作がある場合、HelloWorld は組み込みの 3+4 の合計を返します (今のところハードコードされているだけです)。

1)次の機能を実装しています:

    public IBinder onBind(Intent intent) {
        // Return the interface
        Log.d(getClass().getSimpleName(),"IBinder");
        return mBinder;
    }

2) 私の Android マニフェスト ファイルには、次のエントリがあります。

    <service android:name=".RemoteService"
                android:enabled="true"
                 android:process=":remote">
            <intent-filter >
                <action android:name="com.example.helloworld.RemoteService"/>
            </intent-filter>
        </service>

4) ここに私の RemoteService.java があります

public class RemoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // Return the interface
        Log.d(getClass().getSimpleName(),"IBinder");
        return mBinder;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //Log.d(getClass().getSimpleName(),"onCreate()");
  }

    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getSum(int a,int b){
            //System.out.println("Called");
            Log.d(getClass().getSimpleName(),"getSum called");
            //return android.os.Process.myPid();
            return (a+b);
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {
            // Does nothing
        }
    };

}

以下は、RemoteService を呼び出すクライアント コードです ([追加] ボタンをクリックした場合のみ呼び出されます)。

package com.example.myfirstapp;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.helloworld.IRemoteService;

public class MainActivity extends Activity {

    Intent i = new Intent();
    boolean bound = false;

    IRemoteService mIRemoteService = null;

    ServiceConnection mConnection;

    void initConnection(){
    mConnection = new ServiceConnection() {
        // Called when the connection with the service is established
        public void onServiceConnected(ComponentName className, IBinder service) {

            mIRemoteService = IRemoteService.Stub.asInterface((IBinder)service);
            Log.e("TAG 1", "Service has now connected");
        }

        // Called when the connection with the service disconnects unexpectedly
        public void onServiceDisconnected(ComponentName className) {
            Log.e("TAG 1", "Service has unexpectedly disconnected");
            //System.out.println("Service has unexpectedly disconnected");
            mIRemoteService = null;
        }
    };
    if(mIRemoteService == null)
    {
        Intent it = new Intent();
        it.setAction("com.example.helloworld.RemoteService");
        //binding to remote service
        bound = bindService(it, mConnection, Service.BIND_AUTO_CREATE);
        Log.e("TAG 3", bound ? "SERVICE OBTAINED": "NO SERVICE");
    }
}
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//bound = bindService(i, mConnection, Context.BIND_AUTO_CREATE);
        //Log.e("TAG 2", bound ? "SERVICE OBTAINED": "NO SERVICE");


      //display in short period of time
        Toast.makeText(getApplicationContext(), "Your toast message.",
                              Toast.LENGTH_SHORT).show();
        //display in long period of time
        Toast.makeText(getApplicationContext(), "Your toast message",
                              Toast.LENGTH_LONG).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        initConnection();
        return true;
    }

    /** Called when the user clicks the Send button */
    public void subtractMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        int f = Integer.parseInt(editText.getText().toString().trim());
        editText = (EditText) findViewById(R.id.edit_message1);
        int f1 = Integer.parseInt(editText.getText().toString().trim());
        f = f-f1;
        String message = String.valueOf(f);
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);


    }

    public void multMessage /*addAndSend*/(View view){
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        int f = Integer.parseInt(editText.getText().toString().trim());
        editText = (EditText) findViewById(R.id.edit_message1);
        int f1 = Integer.parseInt(editText.getText().toString().trim());
        f = f*f1;
        String message = String.valueOf(f);
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);


    }

    public void addMessage(View view){
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        int f = Integer.parseInt(editText.getText().toString().trim());
        editText = (EditText) findViewById(R.id.edit_message1);
        int f1 = Integer.parseInt(editText.getText().toString().trim());

        String message;

        if(mIRemoteService == null)
        {
            Intent it = new Intent();
            it.setAction("com.example.helloworld.RemoteService");
            //binding to remote service
            bound = bindService(it, mConnection, Service.BIND_AUTO_CREATE);
            Log.e("TAG 4", bound ? "SERVICE OBTAINED": "NO SERVICE");
        }


        try{
        message = Integer.toString(mIRemoteService.getSum(3,4));
        }
        catch(Exception e)
        {
            //setContentView(R.layout.activity_main);
            Log.e("TAG", "Not Successful");
            f = f+f1;
            message = "Not Successful";
        }

        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}
4

0 に答える 0