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