Android Best Practices 主要内容
来源:互联网 发布:淘宝 足迹 没看过 编辑:程序博客网 时间:2024/09/21 08:58
本文用途:搜索、跟踪阅读进度、概括官方文档内容。
忽略介绍、原理、优缺点等内容。
Running in a Background Service
最有用: IntentService
流程:创建IntentService, 发送请求, 接收结果
Creating a Background Service 原文链接
1. 继承 IntentService
2.override onHandleIntent(Intent workIntent), 不用override其他callbacks 。执行任务的代码就在onHandleIntent()中。
3、添加<service>至<application>
IntentService内部有一个线程、IntentService可以相应多个请求(有队列,逐个完成)、执行完后自动停止。
Sending Work Requests to the Background Service 原文链接
1、创建explicit intent 2、startService()
Reporting Work Status
原文链接 涉及LocalBroadcastManager类
1)IntentService发送结果:1、创建Intent(可自行指定action, data uri) 2、用LocalBroadcastManager发送该intent( sendBroadcast() )
public class RSSPullService extends IntentService {... /* * Creates a new Intent containing a Uri object * BROADCAST_ACTION is a custom Intent action */ Intent localIntent = new Intent(Constants.BROADCAST_ACTION) // Puts the status into the Intent .putExtra(Constants.EXTENDED_DATA_STATUS, status); // Broadcasts the Intent to receivers in this app. LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);...}2)接收结果:1、继承BroadcastReceiver 2、override onReceive() 3、新建 IntentFilter(可多个,不继承) 4、注册(下面)
// Broadcast receiver for receiving status updates from the IntentServiceprivate class ResponseReceiver extends BroadcastReceiver{ // Prevents instantiation private DownloadStateReceiver() { } // Called when the BroadcastReceiver gets an Intent it's registered to receive @Override public void onReceive(Context context, Intent intent) {... //Handle Intents here.... }}4、注册receiver和intent filter:
// Instantiates a new DownloadStateReceiver DownloadStateReceiver mDownloadStateReceiver = new DownloadStateReceiver(); // Registers the DownloadStateReceiver and its intent filters LocalBroadcastManager.getInstance(this).registerReceiver( mDownloadStateReceiver, mStatusIntentFilter); ...注:receiver和intent filter有多种组合,多次调用 registerReceiver() 即可(每次不同组合)。
接收到intent后,不要start activity。
- Android Best Practices 主要内容
- 《Android best practices》笔记
- Android-Best Practices
- android-Permissions Best Practices
- Best practices in Android development
- Best practices in Android development
- Best practices in Android development
- Best practices in Android development
- Android 开发最佳实践 android-best-practices
- Best Practices -
- BEST PRACTICES FOR CONSUMING APIS ON ANDROID
- android-Best Practices for Web Apps
- Android后台处理最佳实践(Best Practices for Background Jobs)
- Android Out of Memory Error: Causes, Solution and Best practices
- Android Out of Memory Error: Causes, Solution and Best practices
- Android支持多屏幕(4)Best Practices
- Android Out of Memory Error Causes, Solution and Best practices
- Android后台处理最佳实践(Best Practices for Background Jobs)
- diff 与 patch
- 排序算法之希尔排序
- javascript 正则验证URI
- dual is not mapped,select ordercode_seq_id.nextval nextvalue from dual
- XP中如何在任务栏处添加快速启动项、任务栏无法添加快捷方式
- Android Best Practices 主要内容
- C#多线程学习(一) 多线程的相关概念
- Android 设置屏幕不待机
- 工业级无线AP是什么
- 性能测试知多少----性能测试分类之我见
- mahout-0.6中naive bayes训练过程
- 验证 js 的特殊字符
- QT画曲线以及QWT在Windows下的配置
- ulimit 管理系统资源的例子