Android官网中关于service 有一节摘录如下,做一个笔记,以后好查看。
Declaring a service in the manifest
To ensure your app is secure, always use an explicit intent when starting or binding your Service and do not declare intent filters for the service. If it's critical that you allow for some amount of ambiguity as to which service starts, you can supply intent filters for your services and exclude the component name from the Intent, but you then must set the package for the intent with setPackage(), which provides sufficient disambiguation for the target service.
(没有真正理解这一段,应该是在manifest文件中使用intent-filter标签过滤不必要的intent)
Additionally, you can ensure that your service is available to only your app by including the android:exported attribute and setting it to "false". This effectively stops other apps from starting your service, even when using an explicit intent.
(总结,在manifest文件中将android:exported设置成false后,其它应用就不能再启动您的service服务了)
Starting a Service
If the service does not also provide binding, the intent delivered with startService()
is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent
for a broadcast (with getBroadcast()
) and deliver it to the service in the Intent
that starts the service. 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 of events using Toast Notifications or Status Bar Notifications.
(总结:在service中,可以使用Toast 或者 StatusBar Notifications通知用户)
Running a Service in the Foreground
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the"Ongoing" heading, which means that the notification cannot be dismissed 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 the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.
To request that your service run in the foreground, call startForeground()
. This method takes two parameters: an integer that uniquely identifies the notification and the Notification
for the status bar. For example:
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 remove the service from the foreground, call stopForeground()
. This method takes a boolean, indicating whether to remove the status bar notification as well. This method does not stop the service. However, if you stop the service while it's still running in the foreground, then the notificationis also removed.
For more information about notifications, see Creating Status Bar Notifications.
(这一节不太了解,以后用到再来查看)
Managing the Lifecycle of a Service