校园助手APP
'爆笑校园'APP专为学生设计,收集了很多校园生活中的趣事和笑话。 #生活乐趣# #日常生活趣事# #日常生活笑话# #日常笑话APP#
最近没有怎么编码,就把断断续续做了半年的毕业设计拿出来回顾一下。
校园助手,分为服务器端与Android客户端,在此主要介绍客户端,服务器是一位大神用node.js写的。
主要实现的功能有:登陆,信息查询,地图,订餐,网页的调用与解析,主要费劲的就是界面。
客户端的框架是参照网上讲解新浪微博客户端的视频。在现在的工作中经常遇到界面更新的不便,但是这个框架就很解决了这个问题,只是刚入门的我还不知道,遇到很多挫折之后才发现这个框架的便捷。下面就开始简单描述一下:
public class MainService extends Service implements Runnable {
private static final String TAG = "MainService";
private static Queue<Task> tasks = new LinkedList<Task>();
private static ArrayList<Activity> appActivities = new ArrayList<Activity>();
public static boolean isRun;
public static UserInfo nowUser;
public static boolean isNetAvailable = false;
@Override
public void onCreate() {
Thread thread = new Thread(this);
thread.start();
isRun = true;
new Thread(new Runnable() {
@Override
public void run() {
isNetAvailable = NetService.getNetWorkState(MainService.this);
}
}).start();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static void addActivity(Activity activity) {
if (!appActivities.isEmpty()) {
for (Activity at : appActivities) {
if (at.getClass().getName()
.equals(activity.getClass().getName())) {
appActivities.remove(at);
break;
}
}
}
appActivities.add(activity);
}
private static Activity getActivityByName(String name) {
if (!appActivities.isEmpty()) {
for (Activity activity : appActivities) {
if (null != activity) {
if (activity.getClass().getName().indexOf(name) > 0) {
return activity;
}
}
}
}
return null;
}
public static void newTask(Task t) {
tasks.add(t);
}
public void run() {
while (isRun) {
Task task = null;
if (!tasks.isEmpty()) {
task = tasks.poll();
if (null != task) {
doTask(task);
}
}
}
}
private void doTask(Task t) {
Message msg = handler.obtainMessage();
msg.what = t.getTaskId();
switch (t.getTaskId()) {
case Task.USER_LOGIN: {
UserInfo loginUser = (UserInfo) t.getTaskParams().get("loginUser");
if (null != loginUser) {
nowUser = LoginService.login(loginUser);
msg.obj = nowUser;
Log.i(TAG, "用户登录————————>" + nowUser.getUserName());
}
break;
}
default:
break;
}
handler.sendMessage(msg);
}
@SuppressLint("HandlerLeak")
public static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
IAssistantActivity activity = null;
switch (msg.what) {
case Task.USER_LOGIN: {
if (null != msg.obj) {
activity = (IAssistantActivity) getActivityByName("LoginActivity");
}
break;
}
default:
break;
}
activity.refresh(msg.obj);
};
};
public static void appExit(Context context) {
for (Activity activity : appActivities) {
if (!activity.isFinishing())
activity.finish();
}
Intent service = new Intent("cn.edu.wit.services.MainService");
context.stopService(service);
}
}
这个主服务基本可以完全复用,根据需求添加一些内容即可。
再来看看界面这边是如何搭建的
public class LoginActivity extends Activity implements IAssistantActivity {
public static final String TAG = "LoginActivity";
private ProgressDialog progressDialog = null;
private Button btn_Login;
private ClearEditText etUserId;
private ClearEditText etPassword;
private CheckBox cbIsRemember;
private CheckBox cbIsAuto;
private ImageButton ib_spinner;
protected View listView;
private PopupWindow pop;
private List<String> userIDs ;
private List<UserInfo> loginedUsers ;
private MyAdapter adapter;
private Animation shake ;
private UserInfoServices userInfoServices = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
init();
}
@Override
public void init() {
if(!MainService.isRun){
Intent service = new Intent(this, MainService.class);
startService(service);
}
userInfoServices = new UserInfoServices(LoginActivity.this);
initData();
initView();
MainService.addActivity(this);
}
private void initView() {
shake = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.shake);
etUserId = (ClearEditText) findViewById(R.id.userid);
etPassword = (ClearEditText) findViewById(R.id.password);
cbIsRemember = (CheckBox) findViewById(R.id.isremember);
cbIsAuto = (CheckBox) findViewById(R.id.isAuto);
TextView tvGetPassword = (TextView) findViewById(R.id.tvGetPassword);
tvGetPassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
tvGetPassword.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(LoginActivity.this, FindPwdActivity.class);
startActivity(intent);
overridePendingTransition(SwitchActivityAnim.rightIn(), SwitchActivityAnim.rightOut());
}
});
btn_Login = (Button) findViewById(R.id.login_ok);
btn_Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String userId = etUserId.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if (!"".equals(userId) && !"".equals(password)) {
Log.i(TAG, "click login");
UserInfo loginUser = new UserInfo();
loginUser = new UserInfo();
loginUser.setUserId(userId);
loginUser.setPassword(password);
newTask(loginUser);
} else {
if ("".equals(userId)) {
etUserId.startAnimation(shake);
etUserId.setHint("学号不能为空");
etUserId.setHintTextColor(Color.RED);
} else {
etPassword.startAnimation(shake);
etPassword.setHint("密码不能为空");
etPassword.setHintTextColor(Color.RED);
}
}
}
});
ib_spinner = (ImageButton) findViewById(R.id.ib_spinner);
ib_spinner.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ListView listView = new ListView(getApplicationContext());
listView.setCacheColorHint(0x00000000);
listView.setVerticalScrollBarEnabled(false);
listView.setBackgroundColor(getResources().getColor(R.color.white));
listView.getBackground().setAlpha(230);
listView.setAdapter(new MyAdapter());
pop = new PopupWindow(listView, etUserId.getWidth()+ib_spinner.getWidth(),
LayoutParams.WRAP_CONTENT, true);
pop.setBackgroundDrawable(new ColorDrawable(0x00000000));
pop.setOutsideTouchable(true);
pop.setFocusable(true);
pop.showAsDropDown(etUserId, 0, -8);
pop.update();
}
});
}
private void newTask(UserInfo loginUser) {
if(!MainService.isNetAvailable){
Toast.makeText(LoginActivity.this, "网络不可用!", Toast.LENGTH_LONG).show();
}else {
Map<String, Object> taskParams = new HashMap<String, Object>();
taskParams.put("loginUser", loginUser);
Task task = new Task(Task.USER_LOGIN, taskParams);
MainService.newTask(task);
showDialg();
}
}
@Override
public void refresh(Object... obj) {
progressDialog.dismiss();
if (null != obj[0]) {
if (obj[0] instanceof Exception) {
Exception exception = (Exception) obj[0];
System.out.println(exception.getMessage());
Toast.makeText(this, "登录失败!", Toast.LENGTH_SHORT).show();
return;
}
UserInfo user = (UserInfo)obj[0];
if (null == user.getUserName() || "".equals(user.getUserName())) {
Toast.makeText(this, "登录失败!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "请核查用户名密码以及网络连接是否可用", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
Toast.makeText(LoginActivity.this, "欢迎 "+user.getUserName()+"童鞋", Toast.LENGTH_SHORT).show();
int isRemember = cbIsRemember.isChecked() ? 1 :0 ;
int isAuto = cbIsAuto.isChecked() ? 1 :0 ;
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(SwitchActivityAnim.fadeIn(), SwitchActivityAnim.bloomOut());
if (1 == isAuto) {
SharedPreferencesUtil.saveLoginUser(LoginActivity.this, user);
userInfoServices.insertUserInfo(user);
}else if (1 == isRemember) {
userInfoServices.insertUserInfo(user);
}
this.finish();
}
}else {
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
}
}
private void initData(){
userIDs = new ArrayList<String>();
loginedUsers = userInfoServices.getAllLoginedUser();
String users = "";
if (null != loginedUsers && loginedUsers.size() > 0) {
for (UserInfo user : loginedUsers) {
userIDs.add(user.getUserId());
users += user.getUserId()+"\t 密码:"+user.getPassword()+"\n";
}
System.out.println(users);
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return userIDs.size();
}
@Override
public Object getItem(int position) {
return userIDs.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.item_userids, parent, false);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
ImageButton delete = (ImageButton) view.findViewById(R.id.delete);
tv_name.setText(userIDs.get(position));
tv_name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
etUserId.setText(userIDs.get(position));
etPassword.setText(loginedUsers.get(position).getPassword());
pop.dismiss();
}
});
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
userIDs.remove(position);
adapter.notifyDataSetChanged();
}
});
return view;
}
}
private void showDialg() {
if (null == progressDialog ) {
progressDialog = new ProgressDialog(this);
}
progressDialog.setMessage("正在获取信息...");
progressDialog.show();
}
}
通过注释应该能够看清楚。
我是通过这个项目来学Android的,所以这里面没有太多的Android处理技巧,由于工作的原因,只是用以前的代码,并没有优化。当让这样也能让自己看到慢慢成长的过程。
本文出自 “年少的风” 博客,谢绝转载!
网址:校园助手APP https://www.yuejiaxmz.com/news/view/617226
相关内容
校园生活助手系统app下载校园二手app
校园生活助手 v1.8
校园生活新助手:揭秘Android跑腿APP如何改变你的校园日常
java/php/node.js/python智慧校园生活助手APP【2024年毕设】
校园生活助手免费版
51校园app官方版
校园生活助手最新版下载
手把手带你撸一个校园APP(一):项目简介
校助app下载