Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从应用程序类向Android服务广播意图时出现问题_Android_Service_Broadcastreceiver - Fatal编程技术网

从应用程序类向Android服务广播意图时出现问题

从应用程序类向Android服务广播意图时出现问题,android,service,broadcastreceiver,Android,Service,Broadcastreceiver,我有一个设置,在Android中运行一个后台服务,附带一个广播接收器,监听特定类型的意图。然后,广播接收器使用这些意图来调用服务中的特定方法。下面是描述服务结构的代码: public class MyService extends Service { private class ServiceBroadcastReceiver extends BroadcastReceiver { private MyService getMyService() {

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

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"); 
     }



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

getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
。。只有在我从活动中调用它时,它才起作用。然而,我希望即使在应用程序第一次启动时也能“做点什么”,所以我在创建MyService并启动它之后,就在onCreate()方法的自定义应用程序子类中放入了该语句。出于某种原因,应用程序启动时,广播在应用程序类代码中不起作用,但如果我将其放入活动中的某些代码中(例如活动的onCreate()方法中),广播就会起作用。在我的应用程序对象中广播意图之前,我可以确认正在调用服务的onCreate()和onStartCommand()方法,但它似乎根本没有得到意图并执行相关工作

任何帮助都将不胜感激

更新:

以下是我如何从应用程序发送广播(请注意,我首先启动服务):


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

您的
应用程序
是在流程中任何其他组件之前创建的。因此在
onCreate()
中没有
Serivce
运行。如果您在调用应用程序的
onCreate()
时发送广播,
服务
没有注册广播接收器,因为它甚至没有创建

所以,换句话说,您的服务没有接收到广播通知,因为广播接收器尚未注册。而且它尚未注册,因为
服务
尚未启动。在应用程序的
onCreate()
按设计返回之前,它不能启动

您可以使用应用程序的
onCreate()
中的
Context.startService()
直接启动服务。您将能够通过
Intent
对所有所需数据进行路径选择


更新问题的更新答案:

当你打电话时:

getApplicationContext().startService(intent);
您要求Android稍后启动服务。服务未立即启动。只是计划晚一点开始。事实上,在您从应用程序的
onCreate()
函数返回之前,它肯定不会启动。这是因为所有服务的生命周期回调函数(如
onCreate()
onStart()
)都是从主UI线程调用的。应用程序的
onCreate
当前正在阻止主UI线程

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

getApplicationContext().startService(intent);
getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
实际上,您要求稍后启动服务,然后立即发送广播。因为这项服务甚至没有机会启动和注册这次广播 通知,它在实际启动时将不会收到任何内容

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

Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setAction(Constants.ACTION_DO_SOMETHING);
getApplicationContext().startService(intent);

看到我上面的更新,我确实尝试在实际广播之前启动服务。
Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setAction(Constants.ACTION_DO_SOMETHING);
getApplicationContext().startService(intent);