http://excel-magic.com/post-103/
http://excel-magic.com/post-103/
http://www.cnblogs.com/gaoxingstyle/archive/2013/03/11/2954110.html
前言 Loaders,装载机,适用于Android3.0以及更高的版本,它提供了一套在UI的主线程中异步加载数据的框架。使用Loaders可以非常简单的在Activity或者Fragment中异步加载数据,一般适用于大量的数据查询,或者需要经常修改并及时展示的数据显示到UI上,这样可以避免查询数据的时候,造成UI主线程的卡顿。 Loaders有以下特点: 可以适用于Activity和Fragment。 可以提供异步的方式加载数据。 监听数据源,当数据改变的时候,将新的数据发布到UI上。 Loaders使用Cursor加载数据,在更改Cursor的时候,会自动重新连接到最后配置的Cursor中读取数据,因此不需要重新查询数据。 在Android中使用Loaders机制,需要多个类和接口的配合,以下是它们大致的关系图,之后的内容会对这几个类或接口进行详细讲解: LoaderManager LoaderManager,装载机管理器。用于在Activity或者Fragment中管理一个或多个Loader实例。在Activity或者Fragment中,可以通过getLoaderManager()方法获取LoaderManager对象,它是一个单例模式。 介绍几个LoaderManager提供的方法,用于管理Loader: Loader<D> initLoader(int id,Bundle bundle,LoaderCallbacks<D> callback):初始化一个Loader,并注册回调事件。 Loader<D> restartLoader(int id,Bundle bundle,LoaderCallbacks<D> callback):重新启动或创建一个Loader,并注册回调事件。 Loader<D> getLoader(int id):返回给定Id的Loader,如果没有找到则返回Null。 void destroyLoader(int id):根据指定Id,停止和删除Loader。 通过上面几个方法的参数可以看到,都有一个id参数,这个Id是Loader的标识,因为LoaderManager可以管理一个或多个Loader,所以必须通过这个Id参数来唯一确定一个Loader。而InitLoader()、restartLoader()中的bundle参数,传递一个Bundle对象给LoaderCallbacks中的onCreateLoader()去获取,下面介绍LoaderCallbacks。 LoaderManager.LoaderCallbacks LoaderCallbacks是LoaderManager和Loader之间的回调接口。它是一个回调接口,所以我们需要实现其定义的三个方法: Loader<D> onCreateLoader(int id,Bundle bundle):根据指定Id,初始化一个新的Loader。 void onLoadFinished(Loader<D> loader,D data):当Loader被加载完毕后被调用,在其中处理Loader获取的Cursor数据。 void onLoaderReset(Loader<D> loader):当Loader被销毁的时候被调用,在其中可以使Loader的数据不可用。 从LoaderCallbacks的声明的几个方法中可以看到,它是一个泛型的接口,需要指定Loader数据的类型。如果是数据源是从一个ContentProvider中获取的,一般直接使用它的子类CursorLoader,下面介绍CursorLoader。 Loader
Android SQLite database and content provider - Tutorial Lars Vogel (c) 2014, 2016 vogella GmbHVersion 5.1,29.06.2016 Table of Contents 1. SQLite and Android 2. SQLite architecture 3. Tutorial: Using SQLite 4. Content provider and sharing data
Android中操作数据库主要有两种方法:使用SQLiteOpenHelper 和使用ContentProvider。 (一)使用SQLiteOpenHelper:一个抽象类,用于提供管理数据库版本并维护创建数据库的接口。其子类必须实现onCreate(SQLiteDatabase)和onUpdate(SQLiteDatabase, int, int)方法,也可以实现可选方法onOpen(SQLiteDatabase)。另外,也必须重写父类的构造函数。 如果数据库存在则这个类将负责打开数据库,如果不存在则创建一个新的数据库。当Android检测到你在引用一个旧数据库(根据版本号判断)时,它将调用onUpdate()方法。 1.添加数据: 》实例化一个继承了SQLiteOpenHelper抽象类的类,(例如为events); 》获取SQLiteDatabase对象:SQLiteDatabase db = events.getWritableDatabase(); 》定义ContentValues的一个对象用于存储数据:ContentValues values = new ContentValues(); 》调用ContentValues的put方法装载数据; 》将数据插入数据库中:db.insertOrThrow(TABLE_NAME, null, values); 其中SQLiteOpenHelper类中的getReadableDatabase()和getWritableDatabase()及close()方法都是同步方法。 顾名思义,如果insertOrThrow()执行失败,它可以抛出一个SQLException类型的异常。无需使用一个throws关键字来声明该异常,因为它是一个RuntimeException,而不是一个检查异常。但是,如果你喜欢,仍然可以在一个try/catch块中处理它,就像处理任何其他异常一样。如果未处理它并且存在一个错误,程序将终止,并将一条回溯信息转储到Android日志中。 2.查询数据: 因为无需因查询而修改数据库,所以可以只调用getReadableDatabase()获得一个只读句柄。 》SQLiteDatabase db = events.getReadableDatabase(); 》Cursor cursor = db.query(TABLE_NAME, FROM, null,null,null,null,ORDER_BY); 》startManagingCursor(cursor); 其中startManagerCursor():告诉活动应根据该活动的生命周期来管理光标的生命周期。例如:当活动被暂停时,它将自动停止用光标,然后在活动重启时重新查询该光标。当活动终止时,所有托管的光标都将关闭。 3:数据绑定 用于数据表中存在大量数据的情况下,可以提高程序的运行速度。 如果要将大量的数据显示在View中,并将这些数据追加在一个字符串中,则可能耗尽所有内存。 ListView与Adapter的结合使用。 如果数据源是在XML中定义的一个数组,则使用ArrayAdapter;如果数据源是一个来自于数据库查询的Cursor对象,则使用SimpleCursorAdapter。 Java代码 public class DatabaseHelper extends SQLiteOpenHelper { //构造,调用父类构造,数据库名字,版本号(传入更大的版本号可以让数据库升级,onUpgrade被调用)
Download PDF https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-2 Abstract Mobile apps that rely on backend servers for their data needs should provide seamless offline capability. To provide this capability, apps must implement a data sync mechanism that takes connection availability, authentication, and battery usage, among other things, in to account. In Part 1, we discussed how to leverage the Android sync adapter framework to implement these features in a sample restaurant app, mainly using content provider. In this part we will explain the remaining pieces, the sync adapter and authenticator. We will also look at how to use Google cloud messaging (GCM) notifications to trigger the data sync with a backend server. Contents Abstract Overview Data Sync Strategy for Restaurant Sample App – Little Chef Sync Adapter Implementation Authenticator Implementation Configuring and Triggering the Sync About the Author Overview If you haven’t already read Part 1, please refer to the following link: https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-1 Part 1 covers the integration of content provider with our sample app, which uses local SQLite database. Though the content provider is optional for sync adapter, it abstracts the data model from other parts of the app and provides a well-defined API for integrating with other components of Android framework (for example, loaders). To fully integrate Android sync adapter framework into our sample app, we need to implement the following pieces: sync adapter, a sync service that links the sync adapter with Android sync framework, authenticator, and an authenticator service to bridge the sync adapter framework and authenticator. For the authenticator we will use a dummy account for demo purposes. Data Sync…
https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-1 Abstract Mobile apps with a backend need to provide offline capability as the devices may not have continuous network access. We also need an efficient way for our app to automatically sync with a backend server. In this article we will take a look at how Android* sync adapter framework can be leveraged for a restaurant sample app to enable seamless data sync. We will discuss how to provide offline capability using content providers and local SQLite database. This will be a 2-part series; part 1 will cover the usage of content provider APIs with a local SQLite database. Contents Abstract. Overview... A Retail Business Restaurant Sample App – Little Chef. Using a Content Provider with local SQLite database. Implementing the RestaurantContentProvider Accessing RestaurantContentProvider using ContentResolver Adding RestaurantContentProvider to app Manifest. About the Author Overview Android* apps that rely on a backend server for data or content need to provide offline capability for a seamless userexperience. This requires us to maintain a local copy of the data model. We also need an efficient way to keep the local data model insync with the one on the backend server. We can achieve both of these features using the standard Android APIs. Content provider APIs can be used to abstract away the data model, and using SQLite APIs we can maintain a device resident SQLite database for a local copy of the data. For efficient data sync with the server, Android provides sync adapter framework APIs to automatically handle network connection drops, background syncing, and scheduling. Additionally, we can hook up…
https://console.developers.google.com/apis 首先创建凭据 包名,keystore文件的sha1码 API管理器中启用相应的api
android studio 菜单 -> buid -> Generate Signed Apk 创建jks后缀的签名件 然后在build.grade 中引用 android { signingConfigs { config01 { keyAlias 'MXXXXE' keyPassword 'XXXXX' storeFile file("E:/XXXXX/focusdict.jks") storePassword 'XXXXX' } } I have not fully fixed this issue, but I think it is related to the following entries in 'Telegram\TMessagesProj\build.gradle': signingConfigs {