hiiii以下是我的登录代码
public class Login extends ActionBarActivity {
// flag for Internet connection status
Boolean isInternetPresent = false;
String shareduid;
// Connection detector class
ConnectionDetector cd;
ImageView imgview;
EditText uname;
EditText pass;
Button create,login;
TextView trouble;
public static final String MyPREFERENCES = "MyPrefs";
public static String userid = null;
SharedPreferences sharedpreferences;
private static final String TAG = "myAppSurun";
//private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
//End Drawer
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
String otpkey,vrfy;
// url to get all products list
private static String url_all_login = "http://xxx/xxx/xxxx/xxx";
//Globalstring
String username =null;
String password = null;
//Global Variable for login state checking
public boolean loginflag = false;
// RelativeLayout relativeLayout=new RelativeLayout(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
cd = new ConnectionDetector(getApplicationContext());
imgview=(ImageView)findViewById(R.id.imageView2);
uname=(EditText)findViewById(R.id.edituser);
pass=(EditText)findViewById(R.id.editpassword);
create=(Button)findViewById(R.id.create);
login=(Button)findViewById(R.id.Login);
trouble=(TextView)findViewById(R.id.trouble_login);
getSupportActionBar().setTitle("Surun Support");
ColorDrawable(Color.parseColor("#F58634")));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F58634")));
//Click Text Animation
final Animation myanim, imganim;
myanim = AnimationUtils.loadAnimation(this, R.anim.link_text_anim);
imganim = AnimationUtils.loadAnimation(this, R.anim.rotate);
uname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
validation.isValid(uname, "^[_a-zA-Z]+(\\.[_a-zA-Z 0-9-]+)*@[a-zA-Z]+(\\.[a-zA-Z]+)*(\\.[a-zA-Z]{2,})$", "Invalid UserName", true);
}
});
pass.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
validation.isValid(pass, "[0-9]{10}", "Invalid Mobile No", true);
}
});
//end of initializing component
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Create Click", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Registration_user.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
});//End of On click for button
//starting font settings this has prone to error try catch is mandatory while setting font(Overriding native font interface).
try {
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(), "fonts/robotoregular.ttf");
create.setTypeface(myTypeface);
} catch (Exception e) {
Log.v(TAG, "Exception " + e);
}
//End of font settings
//Initializing shared preferences
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
//load preferences if exist
Log.v(TAG,""+sharedpreferences);
Log.v(TAG,"Preference is present loading it");
String shareduser = sharedpreferences.getString("User", "");
String sharedpass = sharedpreferences.getString("Password", "");
shareduid = sharedpreferences.getString("userid", "");
String sharedotp = sharedpreferences.getString("otp", "");
String sharedvrfy = sharedpreferences.getString("verified", "");
Log.v(TAG,"uid"+shareduser);
Log.v(TAG,"otp"+sharedpass);
Log.v(TAG,"vrfy"+sharedvrfy);
if ((shareduser.length() > 0) && (sharedpass.length() > 0)) {
//Navigating to main page
Log.v(TAG,"navigate to main");
Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
i.putExtra("user", shareduser);
i.putExtra("pass", sharedpass);
i.putExtra("userid", shareduid);
i.putExtra("otpkey",sharedotp);
i.putExtra("vrfy", sharedvrfy);
//Starting An Activity
startActivity(i);
finish();
} else {
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
if (uname.getText().length() <= 0 || pass.getText().length() <= 0) {
AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("All Fields Are Mandatory");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "Please Enter Correct User Name And Password", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
if (pass.getText().length() > 10) {
Toast toast1 = Toast.makeText(getApplicationContext(), "Four Characters Only...", Toast.LENGTH_SHORT);
toast1.show();
trouble.setVisibility(View.VISIBLE);
//Log.v(TAG,"Not Valid");
}
} else if (pass.getText().length() > 10) {
AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Mobile no Must be 10 digit only ");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "Please Enter Correct User Name And Password", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
} else if (!(validation.isValid(uname, "^[_a-zA-Z]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z]+(\\.[a-zA-Z]+)*(\\.[a-zA-Z]{2,})$", "Invalid UserName", true))) {
AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Email Is Incorrect");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "Email Id Is InCorrect", Toast.LENGTH_SHORT).show();
uname.requestFocus();
}
});
// Showing Alert Message
alertDialog.show();
} else {
//Animate Button load animation from anim/rotate.xml
imgview.startAnimation(imganim);
username = uname.getText().toString().toLowerCase();
password = pass.getText().toString();
//Sending Login Request To Server for validation Using Asynchronus Tasks where username and password as a parameter to method
Log.v(TAG, "Excuting check detail");
new CheckDetail().execute();
//Creating Shared Preferences
}//end of else_if fields are valid
}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Internet is not active.Please Check Your NEtwork Setting");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "Internet Is Inactive", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
}
});//End of On click for button
}//If no shared preferences found
}//End of onCreate function
@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_admin_home, menu);
return true;
}//End of onCreateOptionMenu
@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;
}
//Remove Following Comment To Enable Drawer Toggling On Login Page
/*// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}*/
return super.onOptionsItemSelected(item);
}//End of onOptionItemSelected
//Alert Dialog when User Click Back Button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK) {
//Ask the user if they want to quit
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.quit)
.setMessage(R.string.really_quit)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
Login.this.finish();
}
})
.setNegativeButton("no", null)
.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}//End of Alert Dialog Box
class CheckDetail extends AsyncTask<String,String, String> {
JSONArray datail=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Logging in. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
try {
Log.v(TAG, "In Do in Background");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", username));
params.add(new BasicNameValuePair("pwd", password));
// getting JSON string from URL
JSONObject json =jParser.makeHttpRequest(url_all_login, "POST", params);
//Object Parsing Failed here hence by using trail guide using JSONParser.alternateJSONArray to parse user data.
//Hence not using json instance of object using a BACKUP static variable of Parser class for proccessing.
//To use this backup utility theme the process should be standard and return unique or two out put only or ether way use three logical step
if(json != null) {
// As if login fails it returns object handling fail logic here.
//We can make it general by sending array from server side so we can only use alternateJSONArray variable
}
if(JSONParser.alternateJSONArray != null)
{
Log.v(TAG, "USING BACKUP ARRAY");
//Check your log cat for JSON futher details
for (int jsonArrayElementIndex=0; jsonArrayElementIndex < JSONParser.alternateJSONArray.length(); jsonArrayElementIndex++) {
JSONObject jsonObjectAtJsonArrayElementIndex = JSONParser.alternateJSONArray.getJSONObject(jsonArrayElementIndex);
userid=jsonObjectAtJsonArrayElementIndex.getString("u_id");
otpkey=jsonObjectAtJsonArrayElementIndex.getString("OTP");
vrfy=jsonObjectAtJsonArrayElementIndex.getString("is_verified");
Log.v(TAG,"" +userid);
Log.v(TAG,"" +otpkey);
Log.v(TAG,"" +vrfy);
if(jsonObjectAtJsonArrayElementIndex.getString("email").equals(username) && jsonObjectAtJsonArrayElementIndex.getString("mobile").equals(password))
{
Log.v(TAG,"Login Successful Now setting loginflag true");
loginflag = true;
}
}
}
else
{
loginflag=false;
}
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "Exception at end :" + e.toString());
//Log.e("TAG", "Error......!RecoverIt");
}
return null ;
}
protected void onPostExecute(String result)
{
// dismiss the dialog after getting all products
//super.onPostExecute();
// pDialog.dismiss();
Log.v(TAG,"verification"+vrfy);
Log.v(TAG,"userid"+userid);
/* if(vrfy==false)
{
Intent i = new Intent(getApplicationContext(), Verifyotp.class);
i.putExtra("userid", userid);
i.putExtra("isverified", vrfy);
startActivity(i);
}else*/
if(loginflag==true) {
Log.v(TAG, "Executing Shared Preferences...");
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("User", uname.getText().toString());
editor.putString("Password", pass.getText().toString());
Log.v(TAG, "Userid==================" + userid);
Log.v(TAG, "otpkey==============" + otpkey);
Log.v(TAG, "is verified " + vrfy);
editor.putString("userid", userid);
editor.putString("otp", otpkey);
editor.putString("vrfy", vrfy);
editor.commit();
Toast.makeText(getApplicationContext(), "Login Succeed", Toast.LENGTH_SHORT).show();
android.util.Log.v(TAG, "Login Succeed");
Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
i.putExtra("user", shareduid);
Log.v(TAG, "" + userid);
startActivity(i);
finish();
}
else
{
Toast.makeText(getApplicationContext(),"Login failed,Invalid Details...!",Toast.LENGTH_LONG).show();
trouble.setVisibility(View.VISIBLE);
}
pDialog.dismiss();
}
}
我将loginid从login.java发送到下一个活动,但在其他活动中,它第一次没有收到该用户ID。第一次显示null但在其他地方显示/打印用户ID更正
如果我重新启动应用程序用户ID将是完美的,应用程序将正常工作 我不知道为什么它会在第一时间发生 其他活动中的代码
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
u_id = (String) b.get("userid");//first time it shows null but after restart it get correct value
Log.v(TAG, "userlogged in" + u_id);
}
答案 0 :(得分:0)
因为您在onpost方法
中发送以下第一次空值更改 Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
i.putExtra("user", shareduid);
Log.v(TAG, "" + userid);
startActivity(i);
上面的shareduid中的在您首次按共享首选项设置时为空,因此只需将其放在
下面Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
i.putExtra("user", userid);
Log.v(TAG, "" + userid);
startActivity(i);
check with this and let me know also i think check with key if you putextra with 'user' key you have to access with the same 'user' key