如何在应用程序中实现app活动

时间:2014-04-21 11:18:13

标签: android google-play-services

我有一个应用程序,我从谷歌帐户获取用户数据,用户也可以在他的墙上发布,我想在我的应用程序上添加评论活动。我接受了帮助 点击here!或者这里a link!并按照每个步骤进行操作,但是我必须在Plus.MomentsApi.write(mGoogleApiClient, moment);上写connection handler,但它会出错并强行关闭活动,我不知道是什么问题。请帮我理清一下。

MainActivity

public class MainActivity extends Activity implements OnClickListener,
        ConnectionCallbacks, OnConnectionFailedListener {

    private static final int RC_SIGN_IN = 0;
    String personName,   personPhotoUrl ,personGooglePlusProfile,email;
    private static final String TAG = "MainActivity";


    private static final int PROFILE_PIC_SIZE = 400;


    private GoogleApiClient mGoogleApiClient;

   private Plus.PlusOptions   plusOptions;
    private boolean mIntentInProgress;

    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;

    private SignInButton btnSignIn;
    private Button btnSignOut, btnRevokeAccess,post,mail;
    private ImageView imgProfilePic;
    private TextView txtName, txtEmail;
    private LinearLayout llProfileLayout;
    Person currentPerson;
    Moment moment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        btnSignIn = (SignInButton) findViewById(R.id.signin);
        btnSignOut = (Button) findViewById(R.id.signout);
        post = (Button) findViewById(R.id.postToWall);


        txtName = (TextView) findViewById(R.id.txtName);
        txtEmail = (TextView) findViewById(R.id.txtEmail);
        llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);


        btnSignIn.setOnClickListener(this);
        btnSignOut.setOnClickListener(this);

        post.setOnClickListener(this);
        plusOptions = new Plus.PlusOptions.Builder()
        .addActivityTypes("http://schemas.google.com/ListenActivity")
        .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).addApi(Plus.API, plusOptions)
                .addScope(Plus.SCOPE_PLUS_LOGIN).build();
        ItemScope target = new ItemScope.Builder()
        .setId("myuniqueidforthissong")
        .setName("When Johnny Comes Marching Home")
        .setDescription("A song about missing one's family members fighting in the American Civil War")
        .setImage("http://example.com/images/albumThumb.png")
        .setType("http://schema.org/MusicRecording")
        .build();

  moment = new Moment.Builder()
        .setType("http://schemas.google.com/ListenActivity")
        .setTarget(target)
        .build();
    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            // Plus.MomentsApi.write(mGoogleApiClient, moment);
            mGoogleApiClient.disconnect();
        }
    }


    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        //mLastConnectionResult = result;
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {

            mConnectionResult = result;

            if (mSignInClicked) {

                resolveSignInError();
            }
        }

    }




  public void shareOnGooglePlus(Activity activity, String caption)
    {
      Intent shareIntent = ShareCompat.IntentBuilder.from(MainActivity.this)
               .setText("")
               .setType("text/plain")
               .getIntent()
               .setPackage("com.google.android.apps.plus");

            startActivity(shareIntent);
    } 


    @Override
    protected void onActivityResult(int requestCode, int responseCode,
            Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        Plus.MomentsApi.write(mGoogleApiClient, moment);


        getProfileInformation();

         updateUI(true);

    }


    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            btnSignOut.setVisibility(View.VISIBLE);
           // btnRevokeAccess.setVisibility(View.VISIBLE);
            llProfileLayout.setVisibility(View.VISIBLE);
            post.setVisibility(View.VISIBLE);
          //  mail.setVisibility(View.VISIBLE);
        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            btnSignOut.setVisibility(View.GONE);
          //  btnRevokeAccess.setVisibility(View.GONE);
            llProfileLayout.setVisibility(View.GONE);
            post.setVisibility(View.GONE);
          //  mail.setVisibility(View.GONE);
        }
    }









    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.signin:

            signInWithGplus();
            break;
        case R.id.signout:

            signOutFromGplus();
            break;
        case R.id.postToWall:

            shareOnGooglePlus(this, "caption");


        }
    }


    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }


    private void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
           mGoogleApiClient.connect();
            updateUI(false);
        }
    }

logcat的

04-21 17:02:22.713: E/AndroidRuntime(7121): FATAL EXCEPTION: main
04-21 17:02:22.713: E/AndroidRuntime(7121): java.lang.IllegalStateException: Results have already been set
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.internal.er.a(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.common.api.a$a.a(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.common.api.a$a.b(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.plus.internal.e$d.c(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.plus.internal.e$d.a(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.internal.eh$b.ec(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.google.android.gms.internal.eh$a.handleMessage(Unknown Source)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at android.os.Looper.loop(Looper.java:153)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at android.app.ActivityThread.main(ActivityThread.java:5000)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at java.lang.reflect.Method.invoke(Method.java:511)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-21 17:02:22.713: E/AndroidRuntime(7121):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案