我建立了两个项目:一个是Remote Service Pro,另一个是测试Remote Service Pro。

package com.example.alpha.receiverserrvice.Services;

// Declare any non-default types here with import statements

interface Pay {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    int pay(String username,String password,float money);
}


远程服务在清单中注册:

<service android:name=".Services.PayService"
android:exported="true">
<intent-filter>
    <action android:name="com.example.alpha.alipay"/>
</intent-filter>




远程服务代码如下:

public class PayService extends Service {
private Mybinder mybinder;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    mybinder=new Mybinder();
    return mybinder;
}

@Override
public void onCreate() {
    super.onCreate();
}

private class Mybinder extends Pay.Stub{

    @Override
    public int pay(String username, String password, float money) throws RemoteException {
        return safePay(username,password,money);
    }
}

private int safePay(String username,String password,float money){
    if("gyq".equals(username)&&"123456".equals(password)){
        if(money>100){
            return 200;
        }
        else return 500;
    }
    else return 404;
}
}


并在另一个项目中测试远程服务代码如下:

public class MainActivity extends AppCompatActivity {

private Myconn conn;
private Pay payment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent("com.example.alpha.alipay");
    intent.setPackage("com.example.alpha.receiverserrvice.Services");
    conn=new Myconn();
    bindService(intent, conn, Context.BIND_AUTO_CREATE);

    Button button= (Button) findViewById(R.id.pay);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Payfor(view);
        }
    });
}

public void Payfor(View view) {
    try {
        int resultcode= payment.pay("gyq123", "123456", 300.0f);
        if (200==resultcode) {
            Toast.makeText(this, "pay success.", Toast.LENGTH_SHORT).show();
        }
        else if (resultcode==404) {
            Toast.makeText(this, "username or password is incorrect", Toast.LENGTH_SHORT).show();
        }
        else if (resultcode==500) {
            Toast.makeText(this, "Lack of balance", Toast.LENGTH_SHORT).show();
        }
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private class Myconn implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        payment=Pay.Stub.asInterface(service);
        Log.d("zhifubao.client", "bind remote servoce success.");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

}
}


最后,android studio中的错误如下:

java.lang.NullPointerException: Attempt to invoke interface method 'int com.example.alpha.receiverserrvice.Services.Pay.pay(java.lang.String, java.lang.String, float)' on a null object reference
        at com.example.alpha.testpayservice.MainActivity$override.Payfor(MainActivity.java:47)
        at com.example.alpha.testpayservice.MainActivity$override.access$dispatch(MainActivity.java)
        at com.example.alpha.testpayservice.MainActivity.Payfor(MainActivity.java:0)
        at com.example.alpha.testpayservice.MainActivity$1$override.onClick(MainActivity.java:40)
        at com.example.alpha.testpayservice.MainActivity$1$override.access$dispatch(MainActivity.java)
        at com.example.alpha.testpayservice.MainActivity$1.onClick(MainActivity.java:0)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


因此,当我尝试将远程服务绑定到调用方法时,它总是向我返回空对象引用,我不知道问题出在哪里,如果可以找到它,请告诉。谢谢。

最佳答案

您的包裹名称错误,应该是:

intent.setPackage("com.example.alpha.receiverserrvice");

10-07 12:22