开发者

Does setComponentEnabledSetting in Package Manager start the service?

开发者 https://www.devze.com 2023-02-17 07:10 出处:网络
If i do the following, does it start the service? PackageManag开发者_开发问答er pm = context.getPackageManager();

If i do the following, does it start the service?

PackageManag开发者_开发问答er pm = context.getPackageManager();
pm.setComponentEnabledSetting(
        new ComponentName(context, MyService.class),
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);

If not, what does it do?

http://developer.android.com/reference/android/content/pm/PackageManager.html

i read the javadoc, it just said 'Set the enabled setting'.


If i do the following, does it start the service?

No. Calling startService() starts the service.

If not, what does it do?

Components, like services, can be enabled or disabled. Disabled components cannot be started.

For example, let's suppose you wanted to respond to some system broadcast, but only some of the time. You could have your <receiver> element be disabled in the manifest, then enable it using the code you show above when you need it. That way, you only need to have the receiver responding to the broadcast when it is needed, not all of the time.


If you are looking for a way to start service automatically, you might want to do this:

Add this to manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
....
<receiver android:name=".service.YourReceiver" android:process=":remote"
                android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

And than start service from the receiver...

0

精彩评论

暂无评论...
验证码 换一张
取 消