您的位置:首页 > 移动开发 > Android开发

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服务了)

Starting a Service

If theservice does not also provide binding, the intent delivered with
startService()
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
PendingIntent
for
a broadcast (with
getBroadcast()
)
and deliver it to theservice in the
Intent
that
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 Status
Bar 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 status
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
startForeground()
.
Thismethod takes two parameters: an integer that uniquely identifies thenotification and the
Notification
for
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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: