How to setup Alertbox from BroadcastReceiver

前端 未结 4 1085
面向向阳花
面向向阳花 2020-11-30 15:32

I have implemented alarm in android app. Alarm is working fine. Toast message is visible. Now I want to make Alert Box Notification to user.

4条回答
  •  醉话见心
    2020-11-30 16:08

    I also looking for this solution but after searching lots of thing i did't get the exact ans for the custom dialog. So at this moment i make the custom dialog and pop up automatically when the internet connection gets down. So First of all we need to make a custom layout which we used for pop up so here is my alertforconnectioncheck.xml file

    
    
    
        
    
            
    
                
            
    
            
    
                
    
                
            
    
        
    
    
    

    Now make the Broadcast extendable class:

    public class NetworkChangeReceiver extends BroadcastReceiver {
        String LOG_TAG = "NetworkChangeReceiver";
        public boolean isConnected = false;
        private SharedPreferences.Editor edit;
        private Boolean status;
        @Override
        public void onReceive(final Context context, final Intent intent) {
    
            Log.v(LOG_TAG, "Receieved notification about network status");
            status = isNetworkAvailable(context);
    
            if (status == false) {
    
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.alertforconnectioncheck);
                dialog.setTitle("No Internet Connection...");
                Button dialogButton = (Button) dialog.findViewById(R.id.ok_button);
                dialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        }
    
        private boolean isNetworkAvailable(Context context) {
            ConnectivityManager connectivity = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            if(!isConnected){
                                Log.v(LOG_TAG, "Now you are connected to Internet!");
                                Toast.makeText(context, "Now you are connected to Internet!", Toast.LENGTH_LONG).show();
                                isConnected = true;
                            }
                            return true;
                        }
                    }
                }
            }
            Log.v(LOG_TAG, "You are not connected to Internet!");
            Toast.makeText(context, "You are not connected to Internet!", Toast.LENGTH_LONG).show();
            isConnected = false;
            return false;
        }
    }
    

    Now in MainActivity Class call the Broadcast Receiver class in onCreate:

    private NetworkChangeReceiver receiver;
    IntentFilter filter;
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            receiver = new NetworkChangeReceiver();
            registerReceiver(receiver, filter);  
    

    This is the custom dialog box which appear automatically when the internet gets down and if you have a multiple Activities in the application you must be call it in every activity in the onCreate hope it helps for some one who looking for this solution.

提交回复
热议问题