简体   繁体   English

单一功能可从广播接收器检索数据

[英]Single function to retrive data from Broadcast receiver

I have a function ReadConfig in non activity class which is invoked by a Service. 我在非活动类中具有由服务调用的功能ReadConfig。 This function uses IntentService to read data from a file. 此函数使用IntentService从文件读取数据。

Function in non activity class 非活动类中的功能

//Start of globalVariables
public static boolean received;
public static String actionID = "ACTION_ID";
public static ArrayList<String> configData;
public static BroadcastReceiver configDataReceiver;
//End of globalVariables

public static List<String> ReadConfig(Context context, String configFileName)
{
    configDataReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context arg0, Intent intent)
        {
            if(intent.getAction().equals(actionID))
            {
                configData = intent.getStringArrayListExtra("CONFIGDATA");
                received = true;
            }
        }
    };
    LocalBroadcastManager.getInstance(context)
            .registerReceiver(configDataReceiver,new IntentFilter(actionID));
    Intent workRequest = new Intent(context,IOConfigurations.class);
    workRequest.putExtra("OPERATION","READ");
    workRequest.putExtra("FILENAME",configFileName);
    //Calling intentservice
    context.startService(workRequest);

    //Wait for broadcast receiver to get data and assign to global variables
    while (!received)
    {
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    LocalBroadcastManager.getInstance(context).unregisterReceiver(configDataReceiver);
    return configData;
}

Intent Service 意向服务

    Intent configData = new Intent(actionID);
    configData.putStringArrayListExtra("CONFIGDATA",FileOperations.readFile(fileName));
    LocalBroadcastManager.getInstance(this).sendBroadcast(configData);

The intent service is getting invoked and broadcasts the data but the receiver however doesn't receive any data from Broadcast manager and the while loop continues for ever. 意向服务正在被调用并广播数据,但是接收器却未从广播管理器接收任何数据,并且while循环永远持续下去。

check my comments 检查我的评论

    public static List<String> ReadConfig(Context context, String configFileName)
{
    configDataReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context arg0, Intent intent)
        {
            if(intent.getAction().equals(actionID))
            {
                configData = intent.getStringArrayListExtra("CONFIGDATA");
                received = true;
            }
        }
    };
    LocalBroadcastManager.getInstance(context)
            .registerReceiver(configDataReceiver,new IntentFilter(actionID));
    Intent workRequest = new Intent(context,IOConfigurations.class);
    workRequest.putExtra("OPERATION","READ");
    workRequest.putExtra("FILENAME",configFileName);
    //Calling intentservice
    context.startService(workRequest);

    //Wait for broadcast receiver to get data and assign to global variables
    while (!received)
    {
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

   // Why here your unregistering the broacast listener, 
// Your registering and unregistering in same call, unregiter when your work is done        

LocalBroadcastManager.getInstance(context).unregisterReceiver(configDataReceiver);
    return configData;
}

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

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