- 浏览: 266973 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
androidios2014:
运行有滚没有把分组选项置顶,楼主帮忙给个源码学习7196628 ...
【so easy~】完全仿QQ好友列表,自定义ExpandableListView! -
hnu_hgc:
求代码,3243544018@qq.com
Android释放内存(结束进程) -
hnu_hgc:
求代码工程
Android释放内存(结束进程) -
l657757077:
楼主给个源码学习学习。邮箱:657757077@qq.com
【so easy~】完全仿QQ好友列表,自定义ExpandableListView! -
hao_yh:
楼主,修改后的代码能发我一份吗?感谢。感觉现在是只要界面创建就 ...
【so easy~】完全仿QQ好友列表,自定义ExpandableListView!
1. Service SDK翻译
自己的翻译 英语不好 硬着头皮翻译了些:
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Service服务 是一种能够在后台执行长时间任务 但不提供UI的应用组件
其他的应用组件可开始一个Service服务,是它在后台继续运行 甚至是进程间通信IPC
比如 一个Service可以处理网络传输,音乐播放,文件读写操作,或是使用content provider,这些都是后台运行的
Service
This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running.、
IntentService
This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.
Service
这是所有Service的父类,当你继承它时,一定要建立新的线程去执行Service的任务,因为这个service将会使用你应用的主线程,默认时他会拖慢你应用中正在运行的所有activity。
IntentService
这是一个Service的子类,它使用一个工作线程来一次一个的处理所有开始的请求。这是最好的选择,如果你不要求您的服务同时处理多个请求。你所要做的只是实现onHandleIntent()方法,用来接收处理每个intent请求,这些都是后台运行的。
Because most started services don't need to handle multiple requests simultaneously (which can actually be a dangerous multi-threading scenario), it's probably best if you implement your service using the IntentService class.
因为大部分开始的服务不需要同时处理多个请求(实际上这是一个危险的多线程的情况)。所以使用IntentService类实现服务可能是最好的方式了。
后面是摘自他人的 ,觉得不错转在这里,多多学习
2. Service的调用
(1)Context.startService():Service会经历onCreate -> onStart(如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次 );stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。 注意,多次调用Context.startservice()不会嵌套(即使会有相应的onStart()方法被调用),所以无论同一个服务被启动了多少次,一旦调用Context.stopService()或者stopSelf(),他都会被停止。补充说明:传递给startService()的Intent对象会传递给onStart()方法。调用顺序为:onCreate --> onStart(可多次调用) --> onDestroy。
(2)Context.bindService():Service会经历onCreate() -> onBind(),onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind -> onDestroyed相应退出,所谓绑定在一起就共存亡了 。
补充说明:传递给bindService()的Intent对象会传递给onBind(),传递给unbindService()的Intent对象会传递给onUnbind()方法。 调用顺序为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。
(3)注意事项:在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。还有一点,目前我没有遇到过需要startService和bindService交互使用的情况(我认为不会有这种需求),所以不必去考虑交互的问题,待遇到时再考虑不迟。
(4)BroadcastReceiver只能通过startService启动Service ,因为广播本身生命周期很短,bind的话没有意义:Method bindService can not be called from an BroadcastReceiver component. A pattern you can use to communicate from an BroadcastReceiver to a Service is to call startService(Intent) with the arguments containing the command to be sent, with the service calling its stopSelf(int) method when done executing that command. See the API demo App/Service/Service Start Arguments Controller for an illustration of this. It is okay, however, to use this method from an BroadcastReceiver that has been registered with registerReceiver(BroadcastReceiver, IntentFilter), since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).
3. 示例代码
(1)调用者Activity
- public class TServiceHolder extends Activity {
- private boolean isBound;
- private TService tService;
- private int statusCode;
- private ServiceConnection conn = new ServiceConnection() {
- // Called when a connection to the Service has been established, with the IBinder of the communication channel to the Service.
- public void onServiceConnected(ComponentName name, IBinder service) {
- tService = ( (TService.LocalBinder) service ).getService();
- statusCode = ( (TService.LocalBinder) service ).getStatusCode();
- Toast.makeText(TServiceHolder.this, "Service Connected", Toast.LENGTH_SHORT).show();
- }
- //无法被触发,原因未能找到
- public void onServiceDisconnected(ComponentName name) {
- tService = null;
- Toast.makeText(TServiceHolder.this, "Service DisConnected", Toast.LENGTH_SHORT).show();
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initButtons();
- }
- private void initButtons() {
- Button startButton = (Button) findViewById(R.id.startService);
- Button bindButton = (Button) findViewById(R.id.bindService);
- Button unbindButton = (Button) findViewById(R.id.unbindService);
- Button stopButton = (Button) findViewById(R.id.stopService);
- startButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(TServiceHolder.this, TService.class);
- TServiceHolder.this.startService(i);
- }
- });
- bindButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(TServiceHolder.this, TService.class);
- TServiceHolder.this.bindService(i, conn, Context.BIND_AUTO_CREATE);
- isBound = true;
- }
- });
- unbindButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- if (isBound) {
- TServiceHolder.this.unbindService(conn);
- isBound = false;
- }
- }
- });
- stopButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(TServiceHolder.this, TService.class);
- TServiceHolder.this.stopService(i);
- }
- });
- }
- }
(2)Service
- public class TService extends android.app.Service {
- private final String TAG = "Service";
- private final int NOTIFICATION_ID = 1;
- private NotificationManager nManager;
- private LocalBinder localBinder = new LocalBinder();
- private int statusCode;
- public void onCreate() {
- Log.i(TAG, "Service.onCreate");
- nManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
- showNotification();
- }
- private void showNotification() {
- Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis());
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0);
- n.setLatestEventInfo(this, "任务标题", "任务内容", contentIntent);
- nManager.notify(NOTIFICATION_ID, n); // 任务栏启动
- }
- public void onStart(Intent intent, int startId) {
- Log.i(TAG, "Service.onStart");
- }
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "Service.onBind");
- /*
- * 调用者和Service间是依靠IBinder对象进行通信的,Service的onBind()返回的IBinder对象传递给调用者中ServiceConnection对象的onServiceConnected()方法(以参数形式接收);
- * TService的调用者就可以利用这个IBinder获得TService对象,进而对TService进行操作控制;
- * TService的调用者也可以利用这个IBinder获得其他信息,比如TService的状态statusCode。
- */
- return localBinder;
- }
- /*
- * 当应用程序bind一个Service后,该应用程序和Service之间就能进行互相通信,通常,这种通信的完成依靠于我们定义的一些接口,例如下面的LocalBinder。
- */
- class LocalBinder extends Binder {
- public TService getService() {
- return TService.this; // Service本身
- }
- public int getStatusCode() {
- return statusCode;
- }
- }
- public void onRebind(Intent i) {
- Log.i(TAG, "Service.onRebind");
- }
- public boolean onUnbind(Intent i) {
- Log.i(TAG, "Service.onUnbind");
- return false;
- }
- public void onDestroy() {
- nManager.cancel(NOTIFICATION_ID); // 任务栏关闭
- Log.i(TAG, "Service.onDestroy");
- }
- }
4. 与远程Service通信(进程间Service通信)
如何两个进程间的Service需要进行通信,则需要把对象序列化后进行互相发送。
Android提供了一个 AIDL (Android接口定义语言)工具来处理序列化和通信。这种情况下Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个 stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例:
先创建一个IMyRemoteService.aidl文件,内容如下:
package com.wissen.testApp;
interface IMyRemoteService {
int getStatusCode();
}
如果你正在使用eclipse的 Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类:
package com.wissen.testApp;
class RemoteService implements Service {
int statusCode;
@Override
public IBinder onBind(Intent arg0) {
return myRemoteServiceStub;
}
private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {
public int getStatusCode() throws RemoteException {
return 0;
}
};
…
}
当客户端应用连接到这个Service时,onServiceConnected方法将被调用,客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法:
…
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMyRemoteService myRemoteService = IMyRemoteService.Stub.asInterface(service);
try {
statusCode = myRemoteService.getStatusCode();
} catch(RemoteException e) {
// handle exception
}
Log.i(”INFO”, “Service bound “);
}
…
};
发表评论
-
【so easy】Android手动打包流程,完全详细版!
2012-08-09 12:19 5163由于是先写的word文档,所以排版不好,看不清的朋友可以直 ... -
转:Bitmap造成OOM的讨论与解决方案
2012-07-26 16:08 11136android BitmapFactory的OutOfM ... -
【so easy~】自定义上下menu菜单
2012-06-25 18:14 2346最近做了自定义菜单,许多阅读器的样子,都是点击屏幕中央弹 ... -
【so easy】Tabhost每个tab中放入多个Activity!
2012-04-25 18:14 5041最近做完了一个客户端项目 外面是个tabhost(用于底部t ... -
【so easy~】完全退出
2012-03-08 16:52 1674String packagename = getPac ... -
【BUG解决】WebView cookies清理
2012-03-05 15:21 12015今天在项目中发现一个BUG 在使用新浪微博账户登录应用时,w ... -
Project has no default.properties file! Edit the project properties to set one.
2012-02-20 14:02 1835导入新项目 有时会:Project has no def ... -
【so easy~】完全仿QQ好友列表,自定义ExpandableListView!
2012-01-10 16:14 24878最近,需要做一个可展开的listview,不禁想起了Expan ... -
Android释放内存(结束进程)
2011-12-30 15:45 10122这两天在搞个内存清理小插件,网上很少这方面资料,之前找过清 ... -
【so easy~】 Gallery 模仿Flash广告栏~!附源码
2011-12-19 16:24 5562先上个效果图~ http://androiddada.i ... -
android 获得手机信息
2011-12-02 14:22 1330Android中与电话功能相关的类是 Telephony ... -
【so easy~】 Webview离线功能(优先cache缓存+cache缓存管理)!
2011-11-28 17:05 39623在做Webview显示服务器的html功能时 需要加入离线功能 ... -
转!SQL:char、varchar、text和nchar、nvarchar、ntext的区别
2011-11-21 16:07 1730SQL:char、varchar、text和nchar、n ... -
【so easy~】 底部菜单可移动焦点~!(仿网易新闻等应用)
2011-11-18 10:51 3568最近比较懒惰,也没有更新博客。今天就把刚刚实现的一个小效果分享 ... -
转:shape 渐变!描边!圆角!漂亮的自定义背景
2011-11-09 09:47 3903在实现背景渐变的帖子里 http://androi ... -
ListView item渐变背景(ListView必备!)
2011-11-07 15:32 7082在看他人的代码学习时,无意中发现的。很多应用中的ListVi ... -
Android之系统自带的文字外观设置及实际显示效果图
2011-11-07 14:50 1250Android之系统自带的文字外观设置及实际显示效果图 ... -
【so easy~】 Intent实现 <分享>功能!~
2011-10-31 15:06 1490http://androiddada.iteye.com/ ... -
【so easy~】 GPS的那点儿事儿~!
2011-10-28 14:27 1981http://androiddada.iteye.com/ ... -
Notification实现下载进度显示!
2011-10-25 14:54 1213用惯了Android的人在 ...
相关推荐
附件是安装NumPy教程_详细版,文件绿色安全,请大家放心下载,仅供交流学习使用,无任何商业目的!
语音端点检测及其在Matlab中的实现.zip
使用C#完成一般文档的打印,带有页眉,页脚文档打印,表格打印,打印预览等
directx修复工具 DirectX修复工具(DirectX repair)是系统DirectX组件修复工具,DirectX修复工具主要是用于检测当前系统的DirectX状态,若发现异常情况就可以马上进行修复,非常快捷,使用效果也非常好。
人脸识别的主要算法 其核心算法是 欧式距离算法使用该算法计算两张脸的面部特征差异,一般在0.6 以下都可以被认为是同一张脸 人脸识别的主要步骤 1 获得人脸图片 2 将人脸图片转为128D的矩阵(这个也就是人脸特征的一种数字化表现) 3 保存人脸128D的特征到文件中 4 获取其他人脸转为128D特征通过欧式距离算法与我们保存的特征对比,如果差距在0.6以下就说明两张脸差距比较小
ciscn 全国大学生信息安全竞赛知识问答-CISCN 题库.zip
JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)JAVA+SQL离散数学题库管理系统(源代码+LW+外文翻译)
strcmp函数应用.zip
蓝桥杯单片机第十一届国赛设计题试做
基于MATLAB的pca人脸识别.zip
520.html
JAVA在线考试管理系统(源代码+LW+开题报告+外文翻译+英文文献+答辩PPT)
STR710的定时器编程C语言例子,开发环境为IAR EWARM。.zip
协同过滤算法(Collaborative Filtering)是一种经典的推荐算法,其基本原理是“协同大家的反馈、评价和意见,一起对海量的信息进行过滤,从中筛选出用户可能感兴趣的信息”。它主要依赖于用户和物品之间的行为关系进行推荐。 协同过滤算法主要分为两类: 基于物品的协同过滤算法:给用户推荐与他之前喜欢的物品相似的物品。 基于用户的协同过滤算法:给用户推荐与他兴趣相似的用户喜欢的物品。 协同过滤算法的优点包括: 无需事先对商品或用户进行分类或标注,适用于各种类型的数据。 算法简单易懂,容易实现和部署。 推荐结果准确性较高,能够为用户提供个性化的推荐服务。 然而,协同过滤算法也存在一些缺点: 对数据量和数据质量要求较高,需要大量的历史数据和较高的数据质量。 容易受到“冷启动”问题的影响,即对新用户或新商品的推荐效果较差。 存在“同质化”问题,即推荐结果容易出现重复或相似的情况。 协同过滤算法在多个场景中有广泛的应用,如电商推荐系统、社交网络推荐和视频推荐系统等。在这些场景中,协同过滤算法可以根据用户的历史行为数据,推荐与用户兴趣相似的商品、用户或内容,从而提高用户的购买转化率、活跃度和社交体验。 未来,协同过滤算法的发展方向可能是结合其他推荐算法形成混合推荐系统,以充分发挥各算法的优势。
FTP(File Transfer Protocol)是文件传输协议的简称。 FTP的主要作用,就是让用户连接上一个远程计算机(这些计算机上运行着FTP服务器程序)查看远程计算机有哪些文件,然后把文件从远程计算机上拷到本地计算机,或把本地计算机的文件送到远程计算机去。 目前FTP服务器软件都为国外作品,例如Server_U、IIS,国内成熟的FTP服务器软件很少,有一些如(Crob FTP Server),但从功能上看来远不能和那些流行的服务器软件媲美。
本项目基于深度学习TensorFlow框架,针对滚动轴承故障诊断方法进行研究。项目采用了卷积神经网络(CNN)对轴承振动信号进行特征提取和分类,实现了对滚动轴承不同故障类型的自动诊断。 在技术实现上,项目利用TensorFlow搭建了一个高效的CNN模型,通过多层卷积、池化操作以及全连接层,自动学习轴承振动信号中的故障特征。同时,采用交叉熵损失函数优化模型参数,提高故障识别率。此外,项目还集成了数据预处理、模型训练、测试评估等功能模块,方便用户快速上手并进行实验研究。 经过运行测试,该项目代码运行稳定,诊断效果良好,可广泛应用于滚动轴承故障诊断领域。对于计算机相关专业的在校学生、老师或企业员工来说,该项目是一份难得的高分大作业资源,同时也是小白学习和实际项目借鉴的优秀参考资料。请放心下载使用,为您的学习和工作提供帮助!
超详细的SpringBoot框架入门教程 Spring Boot框架快速入门教程以大量示例讲解了Spring Boot在各类情境中的应用,让大家可以跟着老师的思维和代码快速理解并掌握。适用于Java 开发人员,尤其是初学Spring Boot的人员和需要从传统 Spring 转向 Spring Boot 开发的技术人员。 下边是动力节点的SpringBoot教程非常适合初学入门,讲的非常详细,而且全程无废话!
毕业设计[主机域名]ISPConfig 3.0.1.3_ispconfig3-codepub.zip
matlab开发-用交叉熵优化多变量宏观模型随机多极值优化.zip
矩阵特征值的计算方法.zip