基础代码
activity发出广播
package com.example.app12;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button buttonId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonId = (Button) findViewById(R.id.button_id);
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.zrx.1708");
intent.putExtra("pig","你家的猪跑了");
sendBroadcast(intent);
}
});
}
}
broadcastresciver
package com.example.app12;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.zrx.1708")) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(intent.getStringExtra("pig"));
AlertDialog dialog = builder.create();
builder.setPositiveButton("找找去", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("不找了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Log.i("TAG", "onReceive: 不找了");
}
}
}
作业
LoginActivity
package com.example.practice1;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.practice1.sqliteHelper.MySQLiteHelper;
public class MainActivity extends AppCompatActivity {
private EditText editextLoginPhone;
private EditText editextLoginPassword;
private Button buttonLogin;
private Button buttonRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editextLoginPhone = (EditText) findViewById(R.id.editext_login_phone);
editextLoginPassword = (EditText) findViewById(R.id.editext_login_password);
buttonLogin = (Button) findViewById(R.id.button_login);
buttonRegister = (Button) findViewById(R.id.button_register);
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(this, "person.db", null, 1);
Share.db = mySQLiteHelper.getReadableDatabase();
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = Share.db.rawQuery("select * from users", new String[]{});
if (cursor != null){
while (cursor.moveToNext()){
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String password = cursor.getString(cursor.getColumnIndex("password"));
if (phone.equals(editextLoginPhone.getText().toString()) && password.equals(editextLoginPassword.getText().toString())){
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
}
}
}
cursor.close();
}
});
}
}
RegisterActivity
package com.example.practice1;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.practice1.sqliteHelper.MySQLiteHelper;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegisterActivity extends AppCompatActivity {
private EditText editextRegisterPhone;
private EditText editextRegisterCode;
private Button buttonCode;
private EditText editextRegisterPassword;
private Button buttonRegisterSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(this, "person.db", null, 1);
Share.db = mySQLiteHelper.getReadableDatabase();
editextRegisterPhone = (EditText) findViewById(R.id.editext_register_phone);
editextRegisterCode = (EditText) findViewById(R.id.editext_register_code);
buttonCode = (Button) findViewById(R.id.button_code);
editextRegisterPassword = (EditText) findViewById(R.id.editext_register_password);
buttonRegisterSubmit = (Button) findViewById(R.id.button_register_submit);
editextRegisterPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (editextRegisterPhone.getText().toString() != null && !hasFocus){
buttonCode.setClickable(true);
buttonRegisterSubmit.setClickable(true);
}else if (editextRegisterPhone == null && !hasFocus){
Toast.makeText(RegisterActivity.this, "Phone不能为空", Toast.LENGTH_SHORT).show();
}
}
});
buttonCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int floor = (int) Math.floor(Math.random() * 10);
String sql = "select * from code";
Cursor cursor = Share.db.rawQuery(sql, new String[]{});
if (cursor != null){
cursor.moveToPosition(floor);
String codes = cursor.getString(cursor.getColumnIndex("codes"));
Intent intent = new Intent();
intent.putExtra("codes",codes);
intent.setAction("com.tooth.1708");
sendBroadcast(intent);
editextRegisterCode.setText(codes);
}
cursor.close();
}
});
buttonRegisterSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = Share.db.rawQuery("select * from code", new String[]{});
if (cursor != null){
while(cursor.moveToNext()){
String codes = cursor.getString(cursor.getColumnIndex("codes"));
if (codes.equals(editextRegisterCode.getText().toString())){
Pattern compile = Pattern.compile("[\\w]{6,16}");
Matcher matcher = compile.matcher(editextRegisterPassword.getText().toString());
if (matcher.matches()){
Toast.makeText(RegisterActivity.this, "密码格式正确", Toast.LENGTH_SHORT).show();
Share.db.execSQL("insert into users values(?,?)",new Object[]{editextRegisterPhone.getText().toString(),editextRegisterPassword.getText().toString()});
Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
startActivity(intent);
}else {
Toast.makeText(RegisterActivity.this, "密码格式不正确", Toast.LENGTH_SHORT).show();
}
}
}
}
cursor.close();
}
});
}
}
MySQLiteHelper
package com.example.practice1.sqliteHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteHelper extends SQLiteOpenHelper {
public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users(phone varchar(20),password varchar(20))");
db.execSQL("create table code(codes varchar(20))");
db.beginTransaction();
for (int i = 0; i < 10; i++) {
StringBuffer stringBuffer = new StringBuffer();
for (int j = 0; j < 4; j++) {
stringBuffer.append((int)Math.floor(Math.random()*10));
}
db.execSQL("insert into code(codes) values(?)",new Object[]{stringBuffer.toString()});
}
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
BroadcastReceiver
package com.example.practice1.receiver;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.example.practice1.R;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.tooth.1708")) {
String codes = intent.getStringExtra("codes");
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentText(codes);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setSmallIcon(R.mipmap.ic_launcher);
manager.notify(101,builder.build());
}
}
}