不错的帖子:
Android漂亮蘑菇街UI界面
http://www.eoeandroid.com/thread-211482-1-1.html
Android 3D 乡村赛车 完整代码
http://www.eoeandroid.com/thread-211471-1-1.html
乐看播放器源代码,对写视频播放的人来说非常有用
http://www.eoeandroid.com/thread-211494-1-1.html
--------------------------------帖子正文---------------------
1.首先新建一个Aapp应用程序,还是需要使用到上一章的数据
package cn.itcast.app; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { /** * 监听ContentProvider(内容提供者)中的数据变化 * 为什么要监听? * 例:此时有A应用和B应用,A应用向ContentProvider添加一条数据 * 而此时B应用若想知道A应用做了什么操作,则需ContentProvider通知 * ContentProvider会发出数据变化通知,B应用听过监听ContentProvider得到通知 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void insert(View v){ Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person"); ContentResolver resolver = this.getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "lixiao"); values.put("phone", "123456789"); values.put("amount", "500"); resolver.insert(uri, values); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="insert" android:text="@string/button" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">A应用</string> <string name="button">往内容提供者添加数据</string> </resources>
使用上章的Other程序作用B应用程序,监听ContentProvider
package cn.itcast.other; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person"); //注册一个数据变化监听器 this.getContentResolver().registerContentObserver(uri, true, new PersonContentObserver(new Handler())); } private class PersonContentObserver extends ContentResolver{ public PersonContentObserver(Handler handler) { super(handler); } public void onChange(boolean selfChange) { // TODO Auto-generated method stub Uri uri = Uri.parse("content://cn.itcast.providers.personprovider/person"); Cursor cursor = getContentResolver().query(uri, null, null, null, "personId desc limit 1"); if(cursor.moveToFirst()){ String name = cursor.getString(cursor.getColumnIndex("name")); Log.i("MainActivity", name); } } } }