public abstract class SettingInjectorService
extends Service
java.lang.Object | ||||
↳ | android.content.Context | |||
↳ | android.content.ContextWrapper | |||
↳ | android.app.Service | |||
↳ | android.location.SettingInjectorService |
动态指定注入到系统设置应用程序显示的应用程序设置列表中的首选项的启用状态
For use only by apps that are included in the system image, for preferences that affect multiple apps. Location settings that apply only to one app should be shown within that app, rather than in the system settings. To add a preference to the list, a subclass ofSettingInjectorService
must be declared in the manifest as so:
<service android:name="com.example.android.injector.MyInjectorService" > <intent-filter> <action android:name="android.location.SettingInjectorService" /> </intent-filter> <meta-data android:name="android.location.SettingInjectorService" android:resource="@xml/my_injected_location_setting" /> </service>The resource file specifies the static data for the setting:
<injected-location-setting xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/injected_setting_title" android:icon="@drawable/ic_acme_corp" android:settingsActivity="com.example.android.injector.MySettingActivity" />Here:
getTitle()
value. The title should make it clear which apps are affected by the setting, typically by including the name of the developer. For example, "Acme Corp. ads preferences." getIcon()
value. Typically this will be a generic icon for the developer rather than the icon for an individual app.SettingInjectorService
. The activity should use your own branding to help emphasize to the user that it is not part of the system settings.onCreate()
, and
onGetEnabled()
methods must all be fast. If either is slow, it can delay the display of settings values for other apps as well. Note further that these methods are called on your app's UI thread.
For compactness, only one copy of a given setting should be injected. If each account has a distinct value for the setting, then only
settingsActivity
should display the value for each account.
Constants |
|
---|---|
String |
ACTION_INJECTED_SETTING_CHANGED 客户端应该在其中一个注入设置的值发生变化时进行广播的意图操作,以便设置可以在UI中更新。 |
String |
ACTION_SERVICE_INTENT 必须在子类的清单中声明的Intent操作。 |
String |
ATTRIBUTES_NAME 包含设置属性的XML标签的名称。 |
String |
META_DATA_NAME 元数据标签的名称,用于指定包含设置属性的资源文件。 |
Inherited constants |
---|
From class android.app.Service
|
From class android.content.Context
|
From interface android.content.ComponentCallbacks2
|
Public constructors |
|
---|---|
SettingInjectorService(String name) 构造函数。 |
Public methods |
|
---|---|
final IBinder |
onBind(Intent intent) 将通信信道返回给服务。 |
final void |
onStart(Intent intent, int startId) 此方法已弃用。 改为实施 |
final int |
onStartCommand(Intent intent, int flags, int startId) 每次客户端通过调用 |
Protected methods |
|
---|---|
abstract boolean |
onGetEnabled() 返回 |
abstract String |
onGetSummary() 此方法在API级别21中已被弃用,不再被调用 |
Inherited methods |
|
---|---|
From class android.app.Service
|
|
From class android.content.ContextWrapper
|
|
From class android.content.Context
|
|
From class java.lang.Object
|
|
From interface android.content.ComponentCallbacks2
|
|
From interface android.content.ComponentCallbacks
|
String ACTION_INJECTED_SETTING_CHANGED
客户端应该在其中一个注入设置的值发生变化时进行广播的意图操作,以便设置可以在UI中更新。
常量值:“android.location.InjectedSettingChanged”
String ACTION_SERVICE_INTENT
必须在子类的清单中声明的Intent操作。 用于启动服务以读取设置的动态状态。
常量值:“android.location.SettingInjectorService”
String META_DATA_NAME
元数据标签的名称,用于指定包含设置属性的资源文件。
常量值:“android.location.SettingInjectorService”
SettingInjectorService (String name)
构造函数。
Parameters | |
---|---|
name |
String : used to identify your subclass in log messages |
IBinder onBind (Intent intent)
将通信信道返回给服务。 如果客户端无法绑定到服务,可能会返回null。 返回IBinder
通常是一个复杂的界面已经described using aidl 。
请注意,与其他应用程序组件不同,此处返回的IBinder接口调用可能不会发生在进程的主线程上 。 有关主线程的更多信息可以在Processes and Threads中找到。
Parameters | |
---|---|
intent |
Intent : The Intent that was used to bind to this service, as given to Context.bindService . Note that any extras that were included with the Intent at that point will not be seen here. |
Returns | |
---|---|
IBinder |
Return an IBinder through which clients can call on to the service. |
void onStart (Intent intent, int startId)
此方法已弃用。
改为实施onStartCommand(Intent, int, int)
。
Parameters | |
---|---|
intent |
Intent
|
startId |
int
|
int onStartCommand (Intent intent, int flags, int startId)
每次客户端通过调用startService(Intent)
显式启动服务时,由系统调用,提供它提供的参数以及代表启动请求的唯一整数标记。 不要直接调用这个方法。
为了向后兼容,默认实现调用 onStart(Intent, int)
并返回 START_STICKY
或 START_STICKY_COMPATIBILITY
。
如果您需要您的应用程序在API级别5之前的平台版本上运行,那么可以使用以下模型来处理较早的onStart(Intent, int)
回调。 handleCommand
方法由您酌情执行:
// This is the old onStart method that will be called on the pre-2.0 // platform. On 2.0 or later we override onStartCommand() so this // method will not be called. @Override public void onStart(Intent intent, int startId) { handleCommand(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { handleCommand(intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; }
请注意,系统会在您的服务的主线程上调用它。 服务的主线程与在同一进程中运行的活动发生UI操作的线程相同。 你应该总是避免拖延主线程的事件循环。 当进行长时间运行的操作,网络调用或繁重的磁盘I / O时,应该启动一个新线程,或使用AsyncTask
。
Parameters | |
---|---|
intent |
Intent : The Intent supplied to startService(Intent) , as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY . |
flags |
int : Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY , or START_FLAG_RETRY . |
startId |
int : A unique integer representing this specific request to start. Use with stopSelfResult(int) . |
Returns | |
---|---|
int |
The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the START_CONTINUATION_MASK bits. |
boolean onGetEnabled ()
返回值isEnabled()
。 不应执行不可预测的长操作,如网络访问 - 请参阅类级别javadoc中的运行时注释。
LOCATION_MODE
is
LOCATION_MODE_OFF
.
It is possible that the user may click on the setting before this method returns, so your settings activity must handle the case where it is invoked even though the setting is disabled. The simplest approach may be to simply call
finish()
when disabled.
Returns | |
---|---|
boolean |
the isEnabled() value |
String onGetSummary ()
此方法在API级别21中已弃用。
不要再打电话了
此方法不再被调用,因为任何注入的设置都不再显示状态值。
Returns | |
---|---|
String |
ignored |