1 packagecom.tuisong;2
3 importjava.util.ArrayList;4 importjava.util.List;5
6 importandroid.app.Activity;7 importandroid.content.Context;8 importandroid.content.Intent;9 importandroid.content.SharedPreferences;10 importandroid.os.Bundle;11 importandroid.view.View;12 importandroid.view.View.OnClickListener;13 importandroid.view.inputmethod.InputMethodManager;14 importandroid.widget.Button;15 importandroid.widget.EditText;16 importandroid.widget.RelativeLayout;17
18 importcom.baidu.android.pushservice.PushConstants;19 importcom.baidu.android.pushservice.PushManager;20 importcom.baidu.push.example.PushDemoActivity;21 importcom.baidu.push.example.Utils;22
23 public class Login extendsActivity {24 String User;25 Button bt;26 EditText et;27 SharedPreferences sp;28 SharedPreferences.Editor edtior;29 RelativeLayout layout;30
31 @Override32 protected voidonCreate(Bundle savedInstanceState) {33 super.onCreate(savedInstanceState);34 setContentView(R.layout.activity_login);35 layout =(RelativeLayout) findViewById(R.id.login_layout);36 et =(EditText) findViewById(R.id.et_user);37 bt =(Button) findViewById(R.id.bt_login);38 sp = getSharedPreferences("key", 0);39 edtior =sp.edit();40
41 bt.setOnClickListener(newOnClickListener() {42
43 @Override44 public voidonClick(View arg0) {45 //TODO Auto-generated method stub
46 User =et.getText().toString();47 //把用户名传递出去
48 edtior.putString("toUser", User);49 //保存登陆状态
50 edtior.putString("Login", User);51 //用来标记当前的活动activity以及activity的状态
52 edtior.putString("SSP", User);53 edtior.commit();54 System.out.println("the login in login is "
55 + sp.getString("Login", null));56 PushManager.startWork(getApplicationContext(),57 PushConstants.LOGIN_TYPE_API_KEY, "api_key");58 PushManager.startWork(getApplicationContext(),59 PushConstants.LOGIN_TYPE_API_KEY,60 Utils.getMetaValue(Login.this, "api_key"));61 //在这里设置标签
62 List tags =getTagsList(User);63 System.out.println("User " +User);64 PushManager.setTags(getApplicationContext(), tags);65 Intent intent = newIntent(getApplicationContext(),66 PushDemoActivity.class);67 intent.putExtra("userid", User);68 startActivity(intent);69 finish();70 }71 });72 //为了实现点击空白地方隐藏输入框的效果,监听RelativeLayout的点击事件
73 layout.setOnClickListener(newOnClickListener() {74
75 @Override76 public voidonClick(View v) {77 //TODO Auto-generated method stub
78 if (v.getId() ==R.id.login_layout) {79 InputMethodManager imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);80 et.setCursorVisible(false);//失去光标
81 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);82 }83 }84 });85
86 }87 //使用百度提供的方法来实现
88 private ListgetTagsList(String originalText) {89
90 List tags = new ArrayList();91 int indexOfComma = originalText.indexOf(',');92 String tag;93 while (indexOfComma != -1) {94 tag = originalText.substring(0, indexOfComma);95 tags.add(tag);96
97 originalText = originalText.substring(indexOfComma + 1);98 indexOfComma = originalText.indexOf(',');99 }100
101 tags.add(originalText);102 returntags;103 }104 }