I am using a broadcast receiver to monitor the network changes. And I want to send the network status to the backend through webView as per the change in the status from the receiver. But webView is in an Activity and broadcast is the separate class to make it app level monitoring. How can I send data from that receiver to the activity to update it in the backend?
BroadcastReceiver
public class NetworkChangeReceiver extends BroadcastReceiver {
////"isOffline"
boolean mIsConnected = false;
public static final String BROADCAST = "android.net.conn.CONNECTIVITY_CHANGE";
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(final Context context, final Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
new InternetSpeedTest(context).execute("sampleURL");
}
}
private class InternetSpeedTest
extends AsyncTask<String, Void, String> {
long startTime;
long endTime;
private long takenTime;
Context context;
public InternetSpeedTest(Context context) {
this.context =context;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected String doInBackground(String... paramVarArgs) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
if (cm == null || capabilities == null) {
return null;
}
else if ( capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
startTime = System.currentTimeMillis();
Log.d("TAG", "doInBackground: StartTime" + startTime);
Bitmap bmp = null;
try {
URL ulrn = new URL(paramVarArgs[0]);
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
Bitmap bitmap = bmp;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 99, stream);
byte[] imageInByte = stream.toByteArray();
long lengthbmp = imageInByte.length;
if (null != bmp) {
endTime = System.currentTimeMillis();
Log.d("TAG", "doInBackground: EndTIme" + endTime);
return lengthbmp + "";
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String result) {
if (result != null) {
long dataSize = Integer.parseInt(result) / 1024;
takenTime = endTime - startTime;
double s = (double) takenTime / 1000;
double speed = dataSize / s;
mIsConnected =true;
Log.d("TAG", "onPostExecute: " + "" + new DecimalFormat("##.##").format(speed) + "kb/second");
//context.sendBroadcast(new Intent(BROADCAST));
}
}
}
}
and webView update code in the activity is like
mWebview.evaluateJavascript("common.setOfflineStatus(" + statusfromreceiver + ")",null);
How can I send data from that receiver to the activity to update it in the backend?
Please help me to solve this issue.
question from:
https://stackoverflow.com/questions/65903969/android-sent-value-to-a-webview-from-broadcastreceiver 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…