Php 通过Intent将数据从一个活动发送到另一个活动
//数据未传输到其他活动,并且combine变量未在OnActivityResult()下获取值。您能告诉我如何执行此操作吗?因为我需要在相机活动中传递combine的值Php 通过Intent将数据从一个活动发送到另一个活动,php,android,android-intent,android-studio,android-imageview,Php,Android,Android Intent,Android Studio,Android Imageview,//数据未传输到其他活动,并且combine变量未在OnActivityResult()下获取值。您能告诉我如何执行此操作吗?因为我需要在相机活动中传递combine的值 public class Login extends AppCompatActivity implements OnConnectionFailedListener, View.OnClickListener, ConnectionCallbacks { GoogleApiClient mGoogleApiClient
public class Login extends AppCompatActivity implements OnConnectionFailedListener, View.OnClickListener, ConnectionCallbacks {
GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
SignInButton signIn_btn;
SharedPreferences sharedPreferences;
boolean loggedIn;
private static final int RC_SIGN_IN = 0;
ImageView camerat,galleryt;
String combine;
TelephonyManager tMgr;
String name,email;
// boolean b=true;
ProgressDialog progress_dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// camerat = (ImageView) findViewById(R.id.btn);
//galleryt= (ImageView) findViewById(R.id.btn2);
sharedPreferences = getSharedPreferences("APP", MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean("isLogin", false);
name = sharedPreferences.getString("name", "");
email = sharedPreferences.getString("e_mail", "");
String ID = sharedPreferences.getString("ID", "");
// String mPhoneNumber = tMgr.getLine1Number();
tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
combine = name + "~" + tMgr.getLine1Number() + "~" + email;
System.out.println("sss===" + name + email);
if(loggedIn) {
Intent i = new Intent(Login.this, Camera.class);
System.out.println("chokha " + name + combine);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("combine_data", combine);
// bundle.putString("combine_data2",email);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
finish();
}
else
{
buidNewGoogleApiClient();
customizeSignBtn();
setBtnClickListeners();
progress_dialog = new ProgressDialog(this);
progress_dialog.setMessage("Signing in....");
System.out.println("sss" + name + email);
}
// oncreate ends here
}
/*
Configure sign-in to request the user's ID, email address, and basic profile.
User's ID and basic profile are included in DEFAULT_SIGN_IN.
create and initialize GoogleApiClient object to use Google Sign-In API and the options specified by gso..
*/
private void buidNewGoogleApiClient(){
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
/*
Customize sign-in button. The sign-in button can be displayed in
multiple sizes and color schemes. It can also be contextually
rendered based on the requested scopes. For example. a red button may
be displayed when Google+ scopes are requested, but a white button
may be displayed when only basic profile is requested. Try adding the
Plus.SCOPE_PLUS_LOGIN scope to see the difference.
*/
private void customizeSignBtn(){
signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);
signIn_btn.setSize(SignInButton.SIZE_STANDARD);
signIn_btn.setScopes(gso.getScopeArray());
}
/*
Set on click Listeners on the sign-in sign-out and disconnect buttons
*/
private void setBtnClickListeners(){
// Button listeners
signIn_btn.setOnClickListener(this);
// findViewById(R.id.sign_out_button).setOnClickListener(this);
// findViewById(R.id.disconnect_button).setOnClickListener(this);
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
Toast.makeText(this, "pita", Toast.LENGTH_SHORT).show();
progress_dialog.dismiss();
Toast.makeText(this, "chita", Toast.LENGTH_SHORT).show();
}
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
getSignInResult(result);
/*System.out.println("Changu "+combine);
GetSet o =new GetSet();
o.setCombine(combine);
Intent intent = new Intent(Login.this, Camera.class);
startActivity(intent);
finish();*/
if (resultCode == RESULT_OK) {
progress_dialog.dismiss();
Toast.makeText(this, "mitra", Toast.LENGTH_SHORT).show();
total();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();
gSignIn();
break;
/*
case R.id.sign_out_button:
Toast.makeText(this, "Google Sign Out", Toast.LENGTH_SHORT).show();
gSignOut();
break;
case R.id.disconnect_button:
Toast.makeText(this, "Google Access Revoked", Toast.LENGTH_SHORT).show();
gRevokeAccess();
break;*/
/* case R.id.btn:
camerat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Login.this, Camera.class);
startActivity(intent);
}
});
break;
case R.id.btn2:
camerat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Login.this, Camera.class);
startActivity(intent);
}
});
break;*/
}
}
private void gSignIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
Toast.makeText(this, "zombie", Toast.LENGTH_SHORT).show();
progress_dialog.show();
}
private void gSignOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUI(false);
}
});
}
private void gRevokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUI(false);
}
});
}
private void getSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
String id_token = acct.getIdToken(); //add this code here to save it by use SharedPreferences
// TextView user_name= (TextView)findViewById(R.id.userName);
// TextView email_id= (TextView)findViewById(R.id.emailId);
// user_name.setText("UserName: "+ acct.getDisplayName());
//email_id.setText("Email Id: " + acct.getEmail());
updateUI(true);
SharedPreferences sharedPreferences = getSharedPreferences("APP", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isLogin", true);
editor.putString("name", acct.getDisplayName());
editor.putString("e_mail", acct.getEmail());
editor.putString("ID", acct.getIdToken());
editor.commit();
progress_dialog.dismiss();
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
private void updateUI(boolean signedIn) {
if (signedIn) {
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
// findViewById(R.id.btn).setVisibility(View.VISIBLE);
// findViewById(R.id.btn2).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
// findViewById(R.id.btn).setVisibility(View.GONE);
// findViewById(R.id.btn2).setVisibility(View.GONE);
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
void total()
{
Intent i = new Intent(Login.this, Camera.class);
System.out.println("chokha " + name + combine);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("combine_data", combine);
// bundle.putString("combine_data2",email);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
finish();
}
}
我认为你应该使用的方法是:
public void startActivityForResult (Intent intent, int requestCode)
此外,在开始下一个活动后,不必对原始活动调用finish()
您可以在此处阅读更多信息:将该数据添加到意图本身如何?那也不行吗 比如:
i.putExtra("name","John");
编辑
在第二个活动中,您将获得如下数据:
Intent intent = getIntent();
String myName = intent.getStringExtra("name");
忘记finish(),认为我在上面单独编写的代码与该代码组合没有获得值假设您希望在登录活动中调用onActivityResult,在类活动中调用public final void setResult(int resultCode,Intent data)方法,然后调用finish()。否则,您不会将结果传回登录活动的任何位置。您尝试发送的数据类型是什么?实际上,我想在OnActivityResult下发送combine变量中的combine值,问题是新活动正在打开,但数据未发送声明的
combine
变量在哪里?要传递的数据是哪个?首先检查联合收割机变量中是否有任何值。尝试打印合并变量的值。@EricBrandwein请参见合并变量位于onActivityResult()下,并且合并在登录活动中声明
i.putExtra("name","John");
Intent intent = getIntent();
String myName = intent.getStringExtra("name");