简体   繁体   English

广播接收器中的呼叫服务

[英]Calling Service in Broadcast receiver Issue

When i call my service in a broadcast receiver if i put at the last of onReceive method; 当我在广播接收器中调用服务时,如果放在onReceive方法的末尾; the service got triggered correctly but if i put the call and i do some other things after the service never got triggered. 服务已正确触发,但如果我拨打电话,但服务未触发后又做了其他事情。

Let me explain, if i do this the service got triggered 让我解释一下,如果我这样做,服务就会触发

public class receiver extends BroadcastReceiver {

    @Override
    public void onReceive( Context context, Intent intent ) {

        // OK
        Intent i = new Intent( context, MyService.class );
        context.startService( i );
    }
}

But if i do this, the service never got triggered and i need to do some other things after the service... 但是如果我这样做,该服务将永远不会被触发,并且在该服务之后我需要做其他一些事情...

public class receiver extends BroadcastReceiver {

        @Override
        public void onReceive( Context context, Intent intent ) {
            Intent i = new Intent( context, MyService.class );
            context.startService( i );

            someMethod();
        }
    }

Maybe i need to add some instruction before i call the service ? 也许我需要在致电服务之前添加一些说明? .- Sorry about my english ..对不起,我的英语

But if i do this, the service never got triggered and i need to do some other things after the service... 但是如果我这样做,该服务将永远不会被触发,并且在该服务之后我需要做其他一些事情...

it may help you: 它可以帮助您:

    public class receiver extends BroadcastReceiver {

    @Override
    public void onReceive( Context context, Intent intent ) {
        final Context ctx = context;
        Thread  thread = new Thread(new Runnable(){
              @Override
              public void run(){
                  Intent i = new Intent( context, MyService.class );
                  ctx.startService( i );
              }
        });

       thread.start();

       someMethod();
    }
}

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

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