@subrat_mandal Sorry for the late reply had caught up with some personal work .
For the code please refer to the first link in the previous post , Only deference is link has an activity class .
copy the same code from OnCreate method to OnReceive method in the broadcast listener class
here you go
public class CodeCoverageReceiver extends BroadcastReceiver {
private final Bundle mResults = new Bundle();
private static final String TAG = "EMMA";
private static final boolean LOGD = true;
private java.io.File coverageFile = null;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Tracer.d(TAG, "Dump coverage intent received >> " + intent.getAction());
getcodeCoveragePath();
generateCoverageReport();
if (coverageFile.canRead() && coverageFile.exists()) {
Tracer.d(TAG, "ec file generated and it is valid....");
}
}
private void getcodeCoveragePath() {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "coverage.ec";
coverageFile = new File(baseDir + File.separator + fileName);
}
private void generateCoverageReport() {
if (LOGD)
Tracer.d(TAG, "generateCoverageReport()");
Tracer.d(TAG, "coverageFile = " + coverageFile.getAbsolutePath());
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod(
"dumpCoverageData", coverageFile.getClass(), boolean.class,
boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
while (!coverageFile.canRead()) {
Tracer.d(TAG, "File is getting created.........");
}
Tracer.d(TAG, "Coverage.ec File is created at " + coverageFile.getAbsolutePath());
} catch(
ClassNotFoundException e)
{
reportEmmaError("Is emma jar on classpath?", e);
} catch(
SecurityException e)
{
reportEmmaError(e);
} catch(
NoSuchMethodException e)
{
reportEmmaError(e);
} catch(
IllegalArgumentException e)
{
reportEmmaError(e);
} catch(
IllegalAccessException e)
{
reportEmmaError(e);
} catch(
InvocationTargetException e)
{
reportEmmaError(e);
}
}
private void reportEmmaError(Exception e) {
reportEmmaError("", e);
}
private void reportEmmaError(String hint, Exception e) {
String msg = "Failed to generate emma coverage. " + hint;
Tracer.d(TAG, msg, e);
mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: "
+ msg);
}
}