Android官网中关于service 介绍的重要记录笔记
2015-07-28 19:55
225 查看
Android官网中关于service 有一节摘录如下,做一个笔记,以后好查看。
Declaring aservice in the manifest
Toensure your app is secure, always use an explicit intent when starting orbinding your Service and
do not declare intent filters for theservice. If it's critical that you allow for some amount of ambiguity as towhich service starts, you can supply intent filters for your services andexclude the component name from the Intent,
but you then must set the package for the intentwith setPackage(),
which provides sufficient disambiguation for thetarget service.
(没有真正理解这一段,应该是在manifest文件中使用intent-filter标签过滤不必要的intent)
Additionally, you can ensure that your service is available to onlyyour app by including the android:exportedattribute
and setting itto "false". This effectively stops other appsfrom starting your service, even when using an explicit intent.
(总结,在manifest文件中将android:exported设置成false后,其它应用就不能再启动您的service服务了)
the only mode ofcommunication between the application component and the service. However, ifyou want the service to send a result back, then the client that starts theservice can create a
a broadcast (with
and deliver it to theservice in the
starts theservice. The service can then use the broadcast to deliver a result.
(总结:当使用startService启动service时,可以使用
Bar Notifications.
(总结:在service中,可以使用Toast 或者
StatusBar Notifications通知用户)
bar, which is placed under the"Ongoing" heading, which means that the notification cannot bedismissed unless the service is either stopped or removed from the foreground.
For example,a music player that plays music from a service should be set to run in theforeground, because the user is explicitly aware of its operation. Thenotification in the status bar might indicate the current song
and allow theuser to launch an activity to interact with the music player.
To requestthat your service run in the foreground, call
Thismethod takes two parameters: an integer that uniquely identifies thenotification and the
the status bar. Forexample:
Caution: The integerID you give to
not be 0.
To removethe service from the foreground, call
Thismethod takes a boolean, indicating whether to remove the status barnotification as well. This method does not stop the service. However, if you stopthe service while it's still running in the foreground, then the notificationis also removed.
For moreinformation about notifications, see Creating Status Bar Notifications.
(这一节不太了解,以后用到再来查看)
Declaring aservice in the manifest
Toensure your app is secure, always use an explicit intent when starting orbinding your Service and
do not declare intent filters for theservice. If it's critical that you allow for some amount of ambiguity as towhich service starts, you can supply intent filters for your services andexclude the component name from the Intent,
but you then must set the package for the intentwith setPackage(),
which provides sufficient disambiguation for thetarget service.
(没有真正理解这一段,应该是在manifest文件中使用intent-filter标签过滤不必要的intent)
Additionally, you can ensure that your service is available to onlyyour app by including the android:exportedattribute
and setting itto "false". This effectively stops other appsfrom starting your service, even when using an explicit intent.
(总结,在manifest文件中将android:exported设置成false后,其它应用就不能再启动您的service服务了)
Starting a Service
If theservice does not also provide binding, the intent delivered withstartService()is
the only mode ofcommunication between the application component and the service. However, ifyou want the service to send a result back, then the client that starts theservice can create a
PendingIntentfor
a broadcast (with
getBroadcast())
and deliver it to theservice in the
Intentthat
starts theservice. The service can then use the broadcast to deliver a result.
(总结:当使用startService启动service时,可以使用
PendingIntent获取结果)
Sending Notifications to the User
Once running, a service can notify the user ofevents using Toast Notifications or StatusBar Notifications.
(总结:在service中,可以使用Toast 或者
StatusBar Notifications通知用户)
Running a Service in the Foreground
A foreground service is a service that'sconsidered to be something the user is actively aware of and thus not acandidate for the system to kill when low on memory. A foreground service mustprovide a notification for the statusbar, which is placed under the"Ongoing" heading, which means that the notification cannot bedismissed unless the service is either stopped or removed from the foreground.
For example,a music player that plays music from a service should be set to run in theforeground, because the user is explicitly aware of its operation. Thenotification in the status bar might indicate the current song
and allow theuser to launch an activity to interact with the music player.
To requestthat your service run in the foreground, call
startForeground().
Thismethod takes two parameters: an integer that uniquely identifies thenotification and the
Notificationfor
the status bar. Forexample:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), pendingIntent); startForeground(ONGOING_NOTIFICATION_ID, notification);
Caution: The integerID you give to
startForeground()must
not be 0.
To removethe service from the foreground, call
stopForeground().
Thismethod takes a boolean, indicating whether to remove the status barnotification as well. This method does not stop the service. However, if you stopthe service while it's still running in the foreground, then the notificationis also removed.
For moreinformation about notifications, see Creating Status Bar Notifications.
(这一节不太了解,以后用到再来查看)
Managing the Lifecycle of a Service
相关文章推荐
- Android 开发者必备的八款小工具
- Android开发中使用Service还是Thread?
- Android 实用工具Hierarchy Viewer实战
- Library weibosdkcore not found
- Android Studio-ADB Not Responding
- android.database.sqlite.SQLiteException: near "order": syntax error (code 1):数据库报错
- Android AIDL 小记录
- Android自定义表情键盘与输入法键盘冲突
- 【Android测试】【第五节】LogCat——命令行
- android使用web加载网页的js问题
- android IntentService 简单介绍
- 自定义组合控件
- Android开发中使用Service还是Thread
- Android编程权威指南-第5章:挑战练习
- android JNI 简单demo(2)它JNI demo 写
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- Android ScrollView下拉图片变大
- Android Studio 模拟器中文显示乱码解决方案
- Android NDK使用addr2line符号化得到错误函数名的问题
- AndroidMaterialDesign动画之CircularReveal