开发者

Pass string value from broadcast receiver to a service

I want to pass a string 开发者_StackOverflowvalue from broadcast receiver to a service.If someone could please illustrate with an example. thanks


In your broadcast receiver write like this

@Override
public void onReceive(Context context, Intent intent) {
    Intent intent=new Intent(context,UrService.class);
    intent.putExtra("stringdata","ur String value");
    context.startService(intent);

}   

In your service class get the value like this

@Override
public void onStart(Intent intent, int startId){
    String data=intent.getStringExtra("stringdata");
    // use this value as your wish
}


@prank you don't need to register the service to the intent, the receiver will be register to the intent, and then when the receiver get the call, it will start the service.
By doing this you pass the long task to the service and close the receiver since it can live only for sort time

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜