package com.example.zuoye1016.Receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.w.w")){
Bundle bundle = intent.getBundleExtra("key");
String yzm = bundle.getString("yzm");
Log.i("TAG", "onReceive: "+yzm);
}
}
}
package com.example.zuoye1016.SqliteHelp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
public class Mysqlite extends SQLiteOpenHelper {
public Mysqlite(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table yz(yzm varchar(30))");
sqLiteDatabase.execSQL("create table user(zhanghao varchar(30),mima varchar(30))");
sqLiteDatabase.beginTransaction();
for (int i = 0; i < 10; i++) {
ContentValues contentValues = new ContentValues();
contentValues.put("yzm","1008"+i);
sqLiteDatabase.insert("yz",null,contentValues);
}
sqLiteDatabase.setTransactionSuccessful();
sqLiteDatabase.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
package com.example.zuoye1016;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.zuoye1016.SqliteHelp.Mysqlite;
public class Main2Activity extends AppCompatActivity {
private EditText et1;
private EditText et2;
private Button bt1;
private String zhanghao;
private String mima;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
bt1 = (Button) findViewById(R.id.bt1);
Cursor user = Share.db.query("user", null, null, null, null, null, null);
if (user!=null){
while(user.moveToNext()){
zhanghao = user.getString(user.getColumnIndex("zhanghao"));
mima = user.getString(user.getColumnIndex("mima"));
}
}
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = et1.getText().toString();
String s1 = et2.getText().toString();
if (s.equals(zhanghao)&&s1.equals(mima)){
Toast.makeText(Main2Activity.this, "成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(Main2Activity.this, "账号密码不正确", Toast.LENGTH_SHORT).show();
}
}
});
}
}
package com.example.zuoye1016;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.zuoye1016.Receiver.MyReceiver;
import com.example.zuoye1016.SqliteHelp.Mysqlite;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private EditText user;
private EditText pwd;
private EditText yzmm;
private Button btn;
private Button btnYzm;
private List<String> list = new ArrayList<>();
private MyReceiver myReceiver;
private String yzm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (EditText) findViewById(R.id.user);
pwd = (EditText) findViewById(R.id.pwd);
btn = (Button) findViewById(R.id.btn);
btnYzm = (Button) findViewById(R.id.btn_yzm);
yzmm = (EditText) findViewById(R.id.yzmm);
Mysqlite mysqlite = new Mysqlite(this,"shujuku.db",null,1);
Share.db = mysqlite.getReadableDatabase();
final Cursor yz = Share.db.query("yz", null, null, null, null, null, null);
if (yz!=null){
while(yz.moveToNext()){
yzm = yz.getString(yz.getColumnIndex("yzm"));
list.add(yzm);
}
}
Log.i(TAG, "onCreate: "+yzm);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = yzmm.getText().toString();
if (s.equals("10080")){
Toast.makeText(MainActivity.this, "是数据库中的数据", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
String zhanghao = user.getText().toString();
String mima = pwd.getText().toString();
ContentValues contentValues = new ContentValues();
contentValues.put("zhanghao",zhanghao);
contentValues.put("mima",mima);
Share.db.insert("user",null,contentValues);
}
}
});
myReceiver = new MyReceiver();
btnYzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "onClick: "+user.getText().toString());
String s = user.getText().toString();
if (s .equals(" ")) {
Toast.makeText(MainActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
} else {
yzmm.setText(list.get(0));
sendNotification();
}
Intent intent = new Intent();
intent.setAction("com.w.w");
Bundle bundle = new Bundle();
bundle.putString("yzm", yzm);
intent.putExtra("key", bundle);
sendBroadcast(intent);
}
});
}
private void sendNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText(yzm);
Notification build = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,build);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}
package com.example.zuoye1016;
import android.database.sqlite.SQLiteDatabase;
public class Share {
public static SQLiteDatabase db;
}