最近在准备开题报告,已经很久没再写博客了,明天要开题答辩了,十分紧张,写个博客,放松一下,祝自己明天顺利通过。哈哈!!!!!

前一阵子,学习了一下java安卓开发,做了一个小东西,就是自己用玩的。本来还要写一些关于智能算法和React的程序,但是时间恐怕不太够了,算了,等下次有时间再写吧,反正技多不压身。

程序的目录


ActivityCollector.java

package com.example.myhomework;import android.app.Activity;
import java.util.ArrayList;
import java.util.List;public class ActivityCollector {public static List<Activity> activities = new ArrayList<>();public static void addActivity(Activity activity) {activities.add(activity);}public static void removeActivity(Activity activity) {activities.remove(activity);}public static void finishAll() {for  (Activity activity : activities){if (!activity.isFinishing()){activity.finish();}}}
}

BaseActivity.java

package com.example.myhomework;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;public class BaseActivity extends AppCompatActivity {private ForceOfflineReceiver receiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.v("BaseActivity","广播接收器已创建");ActivityCollector.addActivity(this);}@Overrideprotected void onResume() {super.onResume();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("com.example.myhomework.FORCE_OFFLINE");receiver = new ForceOfflineReceiver();registerReceiver(receiver, intentFilter);Log.v("BaseActivity","广播接收器已接受");}@Overrideprotected void onPause() {super.onPause();if (receiver != null){unregisterReceiver(receiver);receiver = null;}}@Overrideprotected void onDestroy() {super.onDestroy();ActivityCollector.removeActivity(this);}private class ForceOfflineReceiver extends BroadcastReceiver {@Overridepublic void onReceive(final Context context, Intent intent) {AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("Waring");builder.setMessage("You are forced to be offline. Please try to login again.");builder.setCancelable(false);builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ActivityCollector.finishAll();//销毁所有活动Intent intent = new Intent(context, LoginActivity.class);context.startActivity(intent);//重新启动LoginActivity}});builder.show();}}
}

LoginActivity.java

package com.example.myhomework;import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;public class LoginActivity extends BaseActivity {private SharedPreferences pref;private SharedPreferences.Editor editor;private EditText accountEdit;private EditText passwordEdit;private Button login;private Button register;private Button quertData;private CheckBox rememberPass;private MyDatabaseHelper dbHelper;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);
//        隐藏自带标题ActionBar actionBar = getSupportActionBar();if (actionBar != null){actionBar.hide();}pref = PreferenceManager.getDefaultSharedPreferences(this);accountEdit = (EditText) findViewById(R.id.account);passwordEdit = (EditText) findViewById(R.id.password);rememberPass = (CheckBox) findViewById(R.id.remember_pass);login = (Button) findViewById(R.id.login);register = (Button) findViewById(R.id.register);quertData = (Button) findViewById(R.id.query_data);dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);Button createDatabase = (Button) findViewById(R.id.create_database);createDatabase.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dbHelper.getWritableDatabase();}});boolean isRemember = pref.getBoolean("remember_password", false);if (isRemember){//            将账号和密码都设置到文本框中String account = pref.getString("account", "");String password = pref.getString("password", "");accountEdit.setText(account);passwordEdit.setText(password);rememberPass.setChecked(true);}login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String account = accountEdit.getText().toString();String password = passwordEdit.getText().toString();
//                如果账号是admin密码是123456,就认为登陆成功
//                account.equals("admin") && password.equals("123456")if(checkExist(account,password)){editor = pref.edit();if (rememberPass.isChecked()) { //检查复选框是否被选中editor.putBoolean("remember_password", true);editor.putString("account",account);editor.putString("password", password);}else{editor.clear();}editor.apply();Intent intent = new Intent(LoginActivity.this, MainActivity.class);startActivity(intent);finish();}else{Toast.makeText(LoginActivity.this, "account or wassspord is invalid", Toast.LENGTH_SHORT).show();}}});register.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);startActivity(intent);}});quertData.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();Log.d("LoginActivity", "开始遍历");
//                查询表中所有数据Cursor cursor = db.query("Book", null, null, null,null,null,null);if (cursor.moveToFirst()){do{//遍历Cursor对象,取出数据并打印String account = cursor.getString(cursor.getColumnIndex("account"));String password = cursor.getString(cursor.getColumnIndex("password"));Log.d("LoginActivity", "账号"+account+"\t"+"密码"+ password);} while (cursor.moveToNext());}cursor.close();}});}public Boolean checkExist(String a,String p){dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);SQLiteDatabase db = dbHelper.getWritableDatabase();Log.d("LoginActivity", "开始遍历");
//                查询表中所有数据Cursor cursor = db.query("Book", null, null, null,null,null,null);if (cursor.moveToFirst()){do{//遍历Cursor对象,取出数据并打印String account = cursor.getString(cursor.getColumnIndex("account"));String password = cursor.getString(cursor.getColumnIndex("password"));if(a.equals(account) && p.equals(password)){return true;}Log.d("LoginActivity", "账号"+account+"\t"+"密码"+ password);} while (cursor.moveToNext());}cursor.close();return false;}}

MainActivity.java

package com.example.myhomework;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class MainActivity extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button forceOffline = (Button) findViewById(R.id.force_offline);forceOffline.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent("com.example.myhomework.FORCE_OFFLINE");sendBroadcast(intent);Log.v("MainActivity","已发送广播信息");}});}
}

MyDatabaseHelper.java

package com.example.myhomework;import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;public class MyDatabaseHelper extends SQLiteOpenHelper {public static final String CREATE_TABLE = "create table Book ("+"id integer primary key autoincrement, "+"account text,"+"password text)";private Context mContext;public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);mContext = context;}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_TABLE);Toast.makeText(mContext, "Create successed", Toast.LENGTH_SHORT).show();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("drop table if exists Book");onCreate(db);}
}

RegisterActivity.java

package com.example.myhomework;import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class RegisterActivity extends BaseActivity {private MyDatabaseHelper dbHelper;private Button submit;private Button clear;private TextView account;private TextView password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);submit = (Button) findViewById(R.id.submit);clear = (Button) findViewById(R.id.clear);account = (TextView) findViewById(R.id.re_account);password = (TextView) findViewById(R.id.re_password);submit.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();
//              开始添加第一条数据String a =  account.getText().toString();String b = password.getText().toString();Log.d("RegisterActivity",a+"\t"+b);values.put("account",a);values.put("password",password.getText().toString());db.insert("Book", null, values);
//                Log.d("RegisterActivity",account.getText().toString()+"\t"+password.getText().toString());values.clear();Toast.makeText(RegisterActivity.this, "添加完成", Toast.LENGTH_SHORT).show();Intent intent  = new Intent(RegisterActivity.this,LoginActivity.class);startActivity(intent);}});clear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {account.setText("");password.setText("");}});}
}

Titlelayout.java

package com.example.myhomework;import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;public class Titlelayout extends LinearLayout {public Titlelayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);Button titleback = (Button) findViewById(R.id.title_back);Button titleEdit = (Button) findViewById(R.id.title_edit);titleback.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {((Activity) getContext() ).finish();}});titleEdit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(), "人间自有真情在,多给两分行不行?", Toast.LENGTH_LONG).show();}});}
}

布局的程序

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.example.myhomework.Titlelayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="92dp"android:layout_marginTop="30dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginLeft="100dp"android:text="学生登录界面"android:textSize="50sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="60dp"android:layout_marginTop="40dp"android:orientation="horizontal"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:text="Account"android:textSize="18sp" /><EditTextandroid:id="@+id/account"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="60dp"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textSize="18sp"android:text="Password"/><EditTextandroid:id="@+id/password"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="center_vertical"android:inputType="textPassword"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_marginTop="20dp"android:layout_marginLeft="50dp"android:layout_width="match_parent"android:layout_height="wrap_content"><CheckBoxandroid:id="@+id/remember_pass"android:layout_width="wrap_content"android:layout_height="match_parent" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:text="Remember password"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/login"android:layout_width="167dp"android:layout_height="60dp"android:layout_marginLeft="30dp"android:layout_marginTop="20dp"android:layout_marginRight="10dp"android:text="Login" /><Buttonandroid:id="@+id/register"android:layout_width="167dp"android:layout_height="60dp"android:layout_marginLeft="10dp"android:layout_marginTop="20dp"android:layout_marginRight="10dp"android:text="Register" /></LinearLayout><Buttonandroid:id="@+id/create_database"android:layout_marginTop="40dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Create Table" /><Buttonandroid:id="@+id/query_data"android:layout_marginTop="40dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="查询表中数据并打印" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/force_offline"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Send force offline broadcast"/></LinearLayout>

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="92dp"android:layout_marginTop="30dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginLeft="50dp"android:text="学生注册界面"android:textSize="50sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="60dp"android:layout_marginTop="40dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:orientation="horizontal"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:text="姓名"android:textSize="18sp" /><EditTextandroid:id="@+id/re_account"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_marginTop="10dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:layout_height="60dp"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textSize="18sp"android:text="学号"/><EditTextandroid:id="@+id/re_password"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="center_vertical"android:inputType="textPassword"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_marginTop="10dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:layout_height="60dp"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textSize="18sp"android:text="手机号"/><EditTextandroid:id="@+id/re_phone"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="center_vertical"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_marginTop="10dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:layout_height="60dp"><TextViewandroid:layout_width="90dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textSize="18sp"android:text="性别"/><EditTextandroid:id="@+id/re_xingbie"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="center_vertical"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/submit"android:layout_width="167dp"android:layout_height="60dp"android:layout_marginLeft="30dp"android:layout_marginTop="20dp"android:layout_marginRight="10dp"android:text="Submit" /><Buttonandroid:id="@+id/clear"android:layout_width="167dp"android:layout_height="60dp"android:layout_marginLeft="10dp"android:layout_marginTop="20dp"android:layout_marginRight="10dp"android:text="Clear" /></LinearLayout>
</LinearLayout>

titile.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"tools:context=".MainActivity"><Buttonandroid:id="@+id/title_back"android:layout_width="50dp"android:layout_height="65dp"android:layout_gravity="center"android:layout_margin="5dp"android:background="@drawable/back"android:textColor="#fff" /><TextViewandroid:id="@+id/title_text"android:layout_width="0dp"android:layout_height="42dp"android:layout_gravity="center"android:layout_weight="1"android:gravity="center"android:text="请点击右边的按钮-->"android:textColor="#000"android:textSize="24sp" /><Buttonandroid:id="@+id/title_edit"android:layout_width="53dp"android:layout_height="61dp"android:layout_gravity="center"android:layout_margin="5dp"android:background="@drawable/close"android:textColor="#fff" />
</LinearLayout>

结果





我现在还不会用GitHub托管自己的程序,等再过一阵子吧,好烦,好多事,哦,我前两天用springboot和python和浏览器用websocket建立了联系,浏览器发数据,Python接收数据。挺好玩的。继续努力,加油

安卓手机APP 开发相关推荐

  1. 安卓app开发工具_四川智慧社区安卓手机app开发多少钱

    四川智慧社区安卓手机app开发多少钱 注册登录应用公园后,有两种APP制作模式: 1.主题模式: 应用公园平台提供了上百个配置好的APP模板,可以直接使用,把图片文字替换就可以直接使用.如下图所示: ...

  2. 安卓app开发工具_手机APP开发会涉及到哪些知识点呢?

    随着智能手机的产生,许多APP开始衍生,那么手机APP开发涉及到哪些知识呢?米么信息小编整理了以下内容,一起来看看吧! 手机应用主要分为两大类,一类是基于iPhone(ios)系统APP,另一类则是基 ...

  3. 安卓手机软件开发_无代码,手机app软件开发,让人人都是专业开发工程师

    近期,谷歌发布了自己的无代码在线app开发平台,这款全新工具旨在让任何一个人都可以轻松进行手机app软件开发.这样的动作无疑指引着安卓软件开发的未来. 无代码开发手机app其实由来已久,业内反复讨论了 ...

  4. 易安卓手机APP教程

    易安卓简介: E4A-[易安卓]是一款于 2013 年 06 月 01 日正式发布的.定位于为企业.站长.开发者.网络公司.各种手持设备等等基于安卓系统下的 APP 开发的全中文安卓编程语言,本语言的 ...

  5. python手机app开发_H5 手机 App 开发入门:技术篇

    新人学习手机 App 开发,一开始总要选择一条学习路径. 如果你熟悉 Java 语言,可以学习安卓开发:如果熟悉脚本语言(比如 Python 或 Ruby),可以学习 Swift 语言,进行 iOS ...

  6. 手机App开发的基础概念

    App概念: 就是手机上的应用程序,点击图标就能运行,但是它们的底层技术不一样.按照开发技术,App 可以分成三大类. 原生应用(native application,简称 native App) W ...

  7. (转载)H5 手机 App 开发入门:概念篇

    H5 手机 App 开发入门:概念篇 一.H5 的含义 二.原生应用 2.1 概念 2.2 优点 2.3 缺点 三.Web 应用 3.1 概念 3.2 优点和缺点 3.3 Web APP 的劣势 3. ...

  8. (转载)H5 手机 App 开发入门:技术篇

    H5 手机 App 开发入门:技术篇 一.手机 APP 的技术栈 二.WebView 控件 三.原生技术栈 3.1 Xcode 3.3 Android Studio 四.混合技术栈 4.1 框架种类 ...

  9. H5 手机 App 开发入门:概念篇

    手机现在是互联网的最大入口.根据<中国互联网报告>,手机网民已经超过8亿,人均每天上网三个多小时. 毫不奇怪,手机应用软件(mobile application,简称 mobile App ...

最新文章

  1. 解决 java “错误:编码GBK 的不可映射字符”
  2. 命名时取代基优先顺序_【选修五】高中化学重难点知识:有机物的命名方法
  3. 只需5步,轻松创建HTML5离线应用
  4. java string返回_Java的String字符串内容总结
  5. STM8单片机读取18B20温度传感器
  6. 如何用 RFM 模型扒出 B 站优质 UP 主?| 附实战代码
  7. 跟着百度学PHP[3]-PHP中结构嵌套之循环结构与条件结构嵌套
  8. html中居中的三种方式
  9. php 模拟登陆微信,微信公众平台模拟登陆有关问题
  10. WZ-S甲醛传感器使用说明代码应用案例笔记
  11. 关于 Kubernetes中NetworkPolicy(网络策略)方面的一些笔记
  12. 阿里云生态峰会实录(中)
  13. gpg invalid解决方法
  14. 每周一书《用户故事地图》分享!设计、产品、开发必读!
  15. 【张亚飞】 准确、完整地把握Flash动画设计的知识体系——Flash用户入门必读...
  16. 俞敏洪的屌丝逆袭 大学考了三次进北大
  17. blueCove进行蓝牙传输数据
  18. 2019牛客暑期多校训练营(第一场)B Integration 裂项相消 + 积分
  19. 电脑数据删除了还能恢复吗?为你推荐三种超实用的电脑数据恢复方法
  20. MFC常见错误Qualcomm 开启强发 PowerMeter时

热门文章

  1. 在线 PPT 制作工具:Gossip,聚焦内容内在逻辑
  2. Linux/Unix-stty命令详解
  3. 正式员工、合同工和外包人员有什么区别?
  4. 人工智能时代下,Python与C/C++谁将成为人工智能核心算法选择?
  5. 乌鸦搜索算法和粒子集群算法_乌鸦和乌鸦
  6. 【动画图解微积分笔记】 (一) -1.概述 (附B站视频)
  7. oracle 序列和表关联,Oracle 创建和管理表、集群和序列
  8. android模拟器发送短信
  9. c语言作业素数探求实验题,c语言课程设计-素数探求.doc
  10. python中必须要会的四大高级数据类型(字符,元组,列表,字典)