我记得曾经读到过一个广播接收器接口,可以从同步适配器或某些同步进度事件的ResultReceiver中获取。这个接口是内置于SyncAdapter模式中还是自己开发的呢?
我记得曾经读到过一个广播接收器接口,可以从同步适配器或某些同步进度事件的ResultReceiver中获取。这个接口是内置于SyncAdapter模式中还是自己开发的呢?
我刚刚从同步适配器中实现了一个广播接收器,它像钟表一样运行良好!
我使用一个设置为内部类的接收器,并在 onCreate 中调用 registerReceiver,在 onDestroy 中调用 unregisterReceiver 来完成此操作。
由于我有一个策略方法来生成和查询多个线程,因此在 SyncAdapter 运行的开始时,我只有:
Intent intent = new Intent();
intent.setAction(ACTION);
intent.putExtra(SYNCING_STATUS, RUNNING);
context.sendBroadcast(intent);
同步运行结束后,我得到了以下结果:
intent.putExtra(SYNCING_STATUS, STOPPING);
context.sendBroadcast(intent);
在我的Activity中,我声明:
onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
SyncReceiver myReceiver = new SyncReceiver();
RegisterReceiver(myReceiver,ACTION);
}
onDestroy(){
super.onPause();
unRegisterReceiver(myReceiver);
}
public class SyncReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
//do something
}
}
}
对于这种情况,您无需将接收器添加到清单文件中。只需按原样使用即可!
intent.putExtra(SYNCING_STATUS_PROGRESS, ITEMS_PROCESSED_PRECENT)
。然后当Activity看到 SYNC_START
时显示中间进度旋转器。当Activity看到 SYNCING_STATUS_PROGRESS
时显示进度条。 - Diederik有效方法:
在2010年Google IO会议中提出的方法(开发Android REST客户端应用程序)是将列作为标记放入ContentProvider中,以指示正在获取、放置或其他记录。这允许在UI中放置每行旋转器(或其他视觉更改)。您可以通过自定义CursorAdapter驱动ListView来实现。您的ContentProvider需要根据需要更改标志。
无效方法:
您还可以使用SyncStatusObserver - 这几乎是无用的,因为它响应每个状态更改,而不仅仅是您特定的帐户/内容权限对,而且真的不能告诉您任何有关正在同步的内容,也不能区分“同步事件开始”和“同步事件结束”。毫无价值。:P
code
intent.putExtra(SYNCING_STATUS_TIME, 30);code
- Lettings Mall