这2个小demo实现的效果是:
HelloWorld中是一个登录界面,填充用户名、密码去进行登录,去发送一个广播消息,并且在HelloWorld2中接受广播消息进行判断
在HelloWorld2中定义了一个内容提供者暴露接口进行数据库查询登录信息,如果查询有记录,就进行发送广播,然后在HelloWorld中接收广播,然后进行登录成功的界面跳转
————————————————————开始贴代码————————————————————————
首先看HelloWorld登录界面的布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#DEEEE8" android:orientation="vertical" > <TextView android:id="@+id/loginname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textColor="#000000" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/loginname" /> <TextView android:id="@+id/logipassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:text="密码:" android:textColor="#000000" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/logipassword" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/password" android:text="登陆" /> </RelativeLayout>
helloWorldActivity
package com.ktouch.HelloWorld2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class helloWorldActivity extends Activity { /** * 对方的广播接收器的action!!!!!! */ private final static String ACTION = "com.lp.MyBroadcast"; // 登录 private Button btn; // 用户名 private EditText name; // 密码 private EditText password; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** * 获取控件 */ btn = (Button) findViewById(R.id.btn); name = (EditText) findViewById(R.id.name); password = (EditText) findViewById(R.id.password); //点击事件 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //对方的广播接收器的action!!!!!! intent.setAction(ACTION); //传递值 intent.putExtra("name", name.getText().toString()); intent.putExtra("password", password.getText().toString()); //发送广播!!!!!! sendBroadcast(intent); } }); } }
接下来看HelloWorld2的接收广播代码
自己的注册的广播接收器
<receiver android:name=".MyBroadcast" > <intent-filter> <action android:name="com.lp.MyBroadcast"/> </intent-filter>
package com.HelloWorld; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.widget.Toast; public class MyBroadcast extends BroadcastReceiver { //自己的广播接收器action值---接受对方发来的广播!!!!!! private final static String ACTION = "com.lp.MyBroadcast"; //对方的广播接收器的action值!!!! private final static String ACTION1 = "com.lp.MyBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { if(intent != null){ if(ACTION.equals(intent.getAction())){ ContentResolver contentResolver = context.getContentResolver(); //contentProvider签名,得到url地址进行数据库操作 Uri selectUri = Uri.parse("content://com.HelloWorld.MyContentProvider"); //第一个参数,继承ContentProvider的地址 //第三个参数,写where后面的内容-----获取intent携带过来的值 //最后一个参数,写order by后面的内容 Cursor cursor1 = contentResolver.query(selectUri, null, "name='"+intent.getStringExtra("name")+"'"+" and password='"+intent.getStringExtra("password")+"'", null, null); //如果数据库有查询的记录值,就发送给对方的广播接收器的action值!!!! if(cursor1.moveToNext()){ Intent intent1= new Intent(); //设置对方的广播接收器的action值 intent1.setAction(ACTION1); //传递值 intent1.putExtra("result","登陆成功"); //发送广播 context.sendBroadcast(intent1); } else{ Toast.makeText(context,"用户名密码错误", 1).show(); } } } } }
启动HelloWorld2小demo,进行创建数据库,并且插入一条记录(为了简单期间,特地插入了一条记录,为了登录正确使用)
先看HelloWorld2的界面把---
main.xml--界面啥都没有。。。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
看布局界面的代码
Hello
package com.HelloWorld; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.Toast; import com.util.SQLiteHelp; /** * 这个activity里面就是初始化数据库 */ public class Hello extends Activity { public static SQLiteHelp sq; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sq = new SQLiteHelp(this); SQLiteDatabase database = sq.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Name", "Tom3"); values.put("Password", "5000"); long id = database.insert("user", null, values); // toast提示 Toast.makeText(this, "插入成功" + id, 0).show(); // 关闭资源 database.close(); } }
完事接下来就看数据库方面
SQLiteHelp
package com.util; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SQLiteHelp extends SQLiteOpenHelper { public final static int VERSION = 1;// 版本号 /** * 构造方法,初始化数据库 */ public SQLiteHelp(Context context) { super(context, "login.db", null, VERSION); } /** * 创建数据库 表:user 字段:id Name Password */ @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL("create table IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT,Name String,Password String)"); } catch (SQLException ex) { Log.i("sqlliteHelp", "数据库创建失败"); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table if exists user"); this.onCreate(db); } catch (SQLException ex) { Log.i("sqlliteHelp", ""); } } /** * 用于执行增删改操作=---在这个案例中没有用到 */ public void ExecuteSQL(String sql) { SQLiteDatabase db = this.getWritableDatabase(); try { // 执行sql语句 db.execSQL(sql); } catch (SQLException ex) { throw ex; } finally { db.close(); } } /** * 用于完成数据的查询操作---在这个案例中没有用到 */ public Cursor SELECTUser(String condition) throws SQLException { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = null; cursor = db.query("user", new String[] { "id", "name", "password" }, condition, null, null, null, null); return cursor; } }
最后就看暴露的内容提供者了
MyContentProvider
<provider android:name=".MyContentProvider" android:authorities="com.HelloWorld.MyContentProvider"/>
package com.HelloWorld; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; public class MyContentProvider extends ContentProvider{ @Override public int delete(Uri arg0, String arg1, String[] arg2) { return 0; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public boolean onCreate() { //如果数据库初始化话就返回false,如果没有初始化就进行初始化操作 return (Hello.sq == null) ? false : true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); SQLiteDatabase db = Hello.sq.getReadableDatabase(); qb.setTables("user"); Cursor c = qb.query(db, projection, selection, null, null, null, null); //发送通知 c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
在HelloWorld2--MyBroadcast接受到登录界面传来的广播后,查询,数据库有记录后, 在次发送广播给HelloWorld
并且在MyBroadcastReceiver进行处理操作
MyBroadcastReceiver
<receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:name="com.lp.MyBroadcastReceiver"/> </intent-filter> </receiver>
package com.ktouch.HelloWorld2; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.database.Cursor; public class MyBroadcastReceiver extends BroadcastReceiver { //自己的广播接收器的action值 private final static String ACTION = "com.lp.MyBroadcastReceiver"; String result; Cursor cursor; @Override public void onReceive(Context context, Intent intent) { if (intent != null) { if (ACTION.equals(intent.getAction())) { result = intent.getStringExtra("result"); if ("登陆成功".equals(result)) { Intent intent1 = new Intent(context, OkAcitivity.class); // 一个BroadcastReceiver类要调转到activity,必须设置FLAG_ACTIVITY_NEW_TASK intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } } } } }
OkAcitivity
package com.ktouch.HelloWorld2; import android.app.Activity; import android.os.Bundle; public class OkAcitivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ok); } }
++++++++++++++++++++++++++++++++++++++++或者可以进行动态注册*************************************
例如
在HelloWorld2中
将下面的去掉
<!-- <receiver android:name=".MyBroadcast" > <intent-filter> <action android:name="com.lp.MyBroadcast"/> </intent-filter> </receiver> -->
Hello 改成---利用动态注册的方式
package com.HelloWorld; import android.app.Activity; import android.content.ContentValues; import android.content.IntentFilter; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.Toast; import com.util.SQLiteHelp; /** * 这个activity里面就是初始化数据库 */ public class Hello extends Activity { public static SQLiteHelp sq; private MyBroadcast receiver = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sq = new SQLiteHelp(this); SQLiteDatabase database = sq.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Name", "Tom3"); values.put("Password", "5000"); long id = database.insert("user", null, values); // toast提示 Toast.makeText(this, "插入成功" + id, 0).show(); // 关闭资源 database.close(); if (receiver == null) { /** 如果为null,才可以进行注册接收器 */ receiver = new MyBroadcast(); registerReceiver(receiver, new IntentFilter(MyBroadcast.ACTION)); } } }