简体   繁体   English

以编程方式注册BOOT_COMPLETED广播

[英]Programmatically Register For BOOT_COMPLETED Broadcast

I am trying to register my service to startup when the phone boots. 我试图在手机启动时注册我的服务以启动。

I have setup a BOOT_COMPLETED BroadcastReciever in my service class: 我已经在服务类中设置了BOOT_COMPLETED BroadcastReciever:

public int onStartCommand(Intent intent, int flags, int startId)
{
    startService(intent);

    _bootCompletedReciever = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Log.d(TAG, "Got boot completed");
        }
    };


    IntentFilter filter = new IntentFilter("android.intent.action.BOOT_COMPLETED");
    registerReceiver(_bootCompletedReciever, filter);

    return START_NOT_STICKY;
}

However it is not getting called. 但是它没有被调用。 I have the permission set in my manifest: 我在清单中设置了权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Do you know what I am missing in getting this broadcast to fire in my service when the phone boots (without registering for the broadcast in the manifest)? 您知道手机开机时(未在清单中注册该广播)使该广播在我的服务中触发时所缺少的吗?

Answer 回答

I had to use XML in this case to register a class that calls my service on boot: 在这种情况下,我必须使用XML来注册一个在启动时调用我的服务的类:

public class BootBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, S_GPS.class);
        context.startService(service);
    }
}

And in the manifest: 并在清单中:

<receiver android:name=".BroadcastReceivers.BootBroadcastReceiver" android:enabled="true" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

The receiver you are registering there can't survive a reboot, it's impossible, because it is dynamically registered and the registration is lost at reboot. 您在此处注册的接收器无法重新启动,这是不可能的,因为它是动态注册的,并且重新启动时注册会丢失。

What you CAN do, is to register a receiver in the manifest, but set that receiver to DISABLED, use this flag: 您可以做的是在清单中注册一个接收器,但是将该接收器设置为DISABLED,请使用以下标志:

android:enabled=["true" | "false"]

Then you can programatically set it to enabled using the package manager 然后,您可以使用包管理器以编程方式将其设置为启用

context.getPackageManager()
    .setComponentEnabledSetting(ComponentName componentName, int newState, int flags);

From the docs: 从文档:

componentName   The component to enable
newState        The new enabled state for the component. The legal values for this state are: 
        COMPONENT_ENABLED_STATE_ENABLED, COMPONENT_ENABLED_STATE_DISABLED and COMPONENT_ENABLED_STATE_DEFAULT 
        The last one removes the setting, thereby restoring the component's state to whatever was set in it's manifest (or enabled, by default).
flags           Optional behavior flags: DONT_KILL_APP or 0.

See the package manager documentation for more info. 有关更多信息,请参见软件包管理器文档。

Who will start your service on boot up so that onstartcommand() would get called.? 谁将在启动时启动您的服务,以便调用onstartcommand()。
That will not work. 那不管用。 You have to register a receiver statically in manifest and do what you want in onReceive () of receiver. 您必须在清单中静态注册一个接收器,然后在接收器的onReceive()中执行所需的操作。

Please take a look at this post . 请看一下这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM