无法从Application类向Android服务广播意图

Fai*_*sal 2 service android broadcastreceiver

我有一个设置,我在Android中运行后台服务,附加的BroadcastReceiver侦听特定类型的Intents.然后,广播接收器使用这些意图来调用服务中的特定方法.下面是描述服务结构的代码:

public class MyService extends Service {

     private class ServiceBroadcastReceiver extends BroadcastReceiver {


            private MyService getMyService() {
                return MyService.this;
            }

            public IntentFilter getASReceiverFilter() {
                IntentFilter filter = new IntentFilter()
                filter.addAction(Constants.ACTION_DO_SOMETHING);
                return filter;
            }

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                MyService svc = getMyService();
                String intentAction = intent.getAction();
                Log.i(Constants.LOG_TAG, "Now trying to dispatch following action: " + intentAction);
                //Dispatch to the appropriate method in MyService. 
                if (intentAction.equals(AppServiceConstants.ACTION_DO_SOMETHING)) {
                    svc.doSomething();
                } 
            }
        }


    private ServiceBroadcastReceiver mRequestReceiver = new ServiceBroadcastReceiver();


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void onCreate() {

        Log.i(Constants.LOG_TAG, "Service Created!");
        //Register for broadcast messages indicating the add music button was pressed
        mRequestReceiver = new ServiceBroadcastReceiver();
       registerReceiver(mRequestReceiver, mRequestReceiver.getASReceiverFilter()); 
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(Constants.LOG_TAG, "Service Starting ... ");

        // If we get killed, after returning from here, restart
            return START_STICKY;
    }



    /***********************************************************************************
     * Service Dispatch Methods
     */
     protected void doSomething() {
         Log.i(Constants.LOG_TAG, "Doing something"); 
     }



}
Run Code Online (Sandbox Code Playgroud)

我的问题是让这个服务及其广播接收器从我的应用程序中捕获广播的意图.如果我做以下事情....

getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
Run Code Online (Sandbox Code Playgroud)

..它只有在我从一个Activity中调用它时才有效.但是,我希望即使在应用程序第一次启动时也可以"做某事",所以我在创建MyService并启动它之后立即将该语句放在onCreate()方法中我自己的自定义Application子类中.出于某种原因,当应用程序启动时,广播不在Application类代码中工作,但是如果我将它放在活动中的某些代码中(例如在活动的onCreate()方法中),它将起作用.我可以确认在我的Application对象中广播intent之前调用了服务的onCreate()和onStartCommand()方法,但它似乎没有得到意图并执行相关的工作.

任何帮助,将不胜感激.

更新:

这是我从我的应用程序发送广播的方式(注意我首先启动服务):

 public class MyApplication extends Application {
        @Override
        public void onCreate() {
            Intent intent = new Intent(getApplicationContext(), MyService.class);
        getApplicationContext().startService(intent);
    getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

        }

 }
Run Code Online (Sandbox Code Playgroud)

请注意,在我实际进行广播之前,我正在启动服务.该服务不接收广播,但是如果我从稍后启动的活动中进行类似的呼叫,它确实收到了.

ina*_*ruk 6

Application是在流程中的任何其他组件之前创建的.所以onCreate()没有Serivce跑步.如果您在onCreate()调用应用程序时发送广播,Service则没有注册广播接收器,因为它甚至没有创建.

因此,换句话说,您的服务未接收广播通知,因为广播接收器尚未注册.它还没有注册,因为Service还没有开始.它在应用程序onCreate()返回之前无法通过设计启动.

您可以直接Context.startService()从应用程序启动服务onCreate().您将能够通过所有必需的数据路径Intent.


更新了更新问题的答案:

你打电话的时候:

getApplicationContext().startService(intent);
Run Code Online (Sandbox Code Playgroud)

您要求Android稍后启动服务.该服务不会立即启动.它只安排在稍后开始.实际上,在从Application的onCreate()函数返回之前,它肯定不会启动.这是因为所有Service的生命周期回调函数(如onCreate()onStart())都是从主UI线程调用的.而Applicaiton onCreate目前正在阻止主UI线程.

因此,当您使用以下代码行发送广播时:

getApplicationContext().startService(intent);
getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
Run Code Online (Sandbox Code Playgroud)

你实际上要求稍后开始服务,然后立即发送广播.而且由于服务甚至没有机会启动并注册此广播通知,因此在实际启动时它将不会收到任何内容.

您可以更改服务的onStart()代码以处理意图,然后使用以下命令启动服务:

Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setAction(Constants.ACTION_DO_SOMETHING);
getApplicationContext().startService(intent);
Run Code Online (Sandbox Code Playgroud)