如何在android中使用intent或bundle将数据发送给其他参与者

时间:2013-08-30 14:05:20

标签: android multiplayer google-play-games

嗨,朋友,我正试图玩游戏服务示例,即“buttonclicker”, 我想要做的是,在邀请按钮上将当前时间发送给其他参与者,在此示例中为startActivityForResult(intent,requestcode)&另有3个参数startActivityForResult(intent,requestcode,bundle), 我在这里面对问题;

情形1: 我已尝试使用3参数的第二个方法,其中包含我在捆绑中的当前时间,但是当我通过错误运行该示例时

代码:

@Override
    public void onClick(View v)
    {
        Intent intent;


        Bundle BundleTimer = new Bundle();
        BundleTimer.putString("StartTime",millisecond);

        switch (v.getId()) {
            case R.id.button_single_player:
            case R.id.button_single_player_2:
                resetGameVars();
                startGame(false);
                break;
            case R.id.button_sign_in:
                // user wants to sign in
                if (!verifyPlaceholderIdsReplaced())
                {
                    showAlert("Error", "Sample not set up correctly. Please see README.");
                    return;
                }
                beginUserInitiatedSignIn();
                break;
            case R.id.button_sign_out:
                signOut();
                switchToScreen(R.id.screen_sign_in);
                break;
            case R.id.button_invite_players:
                // show list of invitable players
                intent = getGamesClient().getSelectPlayersIntent(1, 3);

                switchToScreen(R.id.screen_wait);
                startActivityForResult(intent, RC_SELECT_PLAYERS, BundleTimer);
                break;
            case R.id.button_see_invitations:
                // show list of pending invitations
                intent = getGamesClient().getInvitationInboxIntent();

                switchToScreen(R.id.screen_wait);
                startActivityForResult(intent, RC_INVITATION_INBOX, BundleTimer);
                break;
            case R.id.button_accept_popup_invitation:
                // user wants to accept the invitation shown on the invitation
                // popup
                // (the one we got through the OnInvitationReceivedListener).
                acceptInviteToRoom(mIncomingInvitationId);
                mIncomingInvitationId = null;
                break;
        }
    }

案例1的logcat:

> 08-30 19:26:28.687: E/AndroidRuntime(13846): FATAL EXCEPTION: main
> 08-30 19:26:28.687: E/AndroidRuntime(13846):
> java.lang.NoSuchMethodError:
> com.example1.playservicedemo1.multiplayer.startActivityForResult 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> com.example1.playservicedemo1.multiplayer.onClick(multiplayer.java:215)
> 08-30 19:26:28.687: E/AndroidRuntime(13846):  at
> android.view.View.performClick(View.java:2485) 08-30 19:26:28.687:
> E/AndroidRuntime(13846):  at
> android.view.View$PerformClick.run(View.java:9080) 08-30 19:26:28.687:
> E/AndroidRuntime(13846):  at
> android.os.Handler.handleCallback(Handler.java:587) 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> android.os.Handler.dispatchMessage(Handler.java:92) 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> android.os.Looper.loop(Looper.java:130) 08-30 19:26:28.687:
> E/AndroidRuntime(13846):  at
> android.app.ActivityThread.main(ActivityThread.java:3687) 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> java.lang.reflect.Method.invokeNative(Native Method) 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> java.lang.reflect.Method.invoke(Method.java:507) 08-30 19:26:28.687:
> E/AndroidRuntime(13846):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
> 08-30 19:26:28.687: E/AndroidRuntime(13846):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-30
> 19:26:28.687: E/AndroidRuntime(13846):    at
> dalvik.system.NativeStart.main(Native Method)

情况2: 在这里我尝试使用intent.putextra(“statrtime”,毫秒)意图发送数据 但是当我尝试在运行相同示例代码的其他应用程序中检索此额外值时, 它通过来自意图的getextra上的nullpointer;

代码

 @Override
    public void onClick(View v)
    {
        Intent intent;


        switch (v.getId()) {
            case R.id.button_single_player:
            case R.id.button_single_player_2:
                resetGameVars();
                startGame(false);
                break;
            case R.id.button_sign_in:
                // user wants to sign in
                if (!verifyPlaceholderIdsReplaced())
                {
                    showAlert("Error", "Sample not set up correctly. Please see README.");
                    return;
                }
                beginUserInitiatedSignIn();
                break;
            case R.id.button_sign_out:
                signOut();
                switchToScreen(R.id.screen_sign_in);
                break;
            case R.id.button_invite_players:
                // show list of invitable players
                intent = getGamesClient().getSelectPlayersIntent(1, 3);
                intent.putExtra("StartTime", millisecond);
                switchToScreen(R.id.screen_wait);
                startActivityForResult(intent, RC_SELECT_PLAYERS);
                break;
            case R.id.button_see_invitations:
                // show list of pending invitations
                intent = getGamesClient().getInvitationInboxIntent();
                intent.putExtra("StartTime", millisecond);
                switchToScreen(R.id.screen_wait);
                startActivityForResult(intent, RC_INVITATION_INBOX);
                break;
            case R.id.button_accept_popup_invitation:
                // user wants to accept the invitation shown on the invitation
                // popup
                // (the one we got through the OnInvitationReceivedListener).
                acceptInviteToRoom(mIncomingInvitationId);
                mIncomingInvitationId = null;
                break;
        }
    }

案例2的logcat:

> 08-30 19:01:46.264: E/AndroidRuntime(25770):
> java.lang.NullPointerException: println needs a message 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> android.util.Log.println_native(Native Method) 08-30 19:01:46.264:
> E/AndroidRuntime(25770):  at android.util.Log.i(Log.java:159) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> com.example1.playservicedemo1.multiplayer.onInvitationReceived(multiplayer.java:573)
> 08-30 19:01:46.264: E/AndroidRuntime(25770):  at
> com.google.android.gms.internal.bj$n.a(Unknown Source) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> com.google.android.gms.internal.bj$n.a(Unknown Source) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> com.google.android.gms.internal.p$b.p(Unknown Source) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> com.google.android.gms.internal.p$a.handleMessage(Unknown Source)
> 08-30 19:01:46.264: E/AndroidRuntime(25770):  at
> android.os.Handler.dispatchMessage(Handler.java:99) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> android.os.Looper.loop(Looper.java:137) 08-30 19:01:46.264:
> E/AndroidRuntime(25770):  at
> android.app.ActivityThread.main(ActivityThread.java:5103) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> java.lang.reflect.Method.invokeNative(Native Method) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> java.lang.reflect.Method.invoke(Method.java:525) 08-30 19:01:46.264:
> E/AndroidRuntime(25770):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
> 08-30 19:01:46.264: E/AndroidRuntime(25770):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-30
> 19:01:46.264: E/AndroidRuntime(25770):    at
> dalvik.system.NativeStart.main(Native Method)

在这个例子中,我想向选定的玩家发送持续时间,这样如果他们在那段时间后没有回应,他们就不会为那个请求而玩,如果他们在那段时间内回应那么他们就可以玩游戏,

朋友我被困在这,请建议如何完成这项工作, 抱歉英文不好, 谢谢

1 个答案:

答案 0 :(得分:0)

你无法用意图来做到这一点。要在不同的手机之间进行通信,您需要使用可以将设备连接在一起的东西。这可以是网络服务器,蓝牙,本地wifi网络,但在你可以做之前必须在不同的手机之间建立连接。