You can use:
if(text != null) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("BROADCAST").setMessage(text).create();
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
Or to check empty string:
if(!text.equals("") {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("BROADCAST").setMessage(text).create();
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
Or both:
if(text != null && !text.equals("") {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("BROADCAST").setMessage(text).create();
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
Edit: if you're trying to check if the string is equal to the word "null" then your original code looks fine. I would suggest using the debugger or printing some log statements to check what's going on.
So:
private static final String TAG = "SomeActivity";
if (!text.equals("null")) {
// Not equal to the word null
Log.d(TAG, "text value should NOT be null and is: " + text);
//AlertDialog alertDialog = new AlertDialog.Builder(this)
// .setTitle("BROADCAST").setMessage(text).create();
// alertDialog.getWindow().setType(
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
// alertDialog.show();
} else {
// don't show
Log.d(TAG, "text value should be null and is: " + text);
}
But really this is just testing that you've not made a mistake. Further to this, I would recommend not using "null" to indicate the empty string. It would likely confuse anyone else looking at the code. Stick with "" which is more standard.