当互联网关闭并且用户尝试发送邮件时,应用程序崩溃。需要防止崩溃

When internet turns off and user tries to send the mail, app crashes. Need to prevent the crash

提问人:thebitanpaul 提问时间:2/16/2023 最后编辑:thebitanpaul 更新时间:2/16/2023 访问量:16

问:

我已经在Asynctask中实现了邮件发送功能。当互联网可用并且用户尝试发送邮件时,邮件正在发送。但是,如果互联网关闭并且用户尝试发送邮件,则应用程序会崩溃。如何处理这个问题,使应用程序在任何情况下都不会崩溃?


    WebView webView, webView1, webView2;
    WebViewClient webViewClient;
    EditText uid, concern, rno;
    TextView contact, reel, raise, hostel;
    ImageView AIML, BD, CC, IOT, CSE;
    AsyncTask mytask;
    public Session session;
    public String username;
    public String password;
    public String subject;
    public String messageToSend;

    public String[] blocks = {"D1 ", "D2 ", "D3 ", "D4 ", "D5 "};
    public String[] floors = {"1st floor  ", "2nd floor  ", "3rd floor  ", "4th floor  ", "5th floor  "};
    public String itemblock;
    public String itemfloor;
    AutoCompleteTextView autoCompleteTextViewblock, autoCompleteTextViewfloor;
    ArrayAdapter<String> adapterItems;

    public static int interval;
    public static Timer timer;


    public NotesFragment(){
    }



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        View v= inflater.inflate(R.layout.fragment_notes, container, false);

        AIML = v.findViewById(R.id.AIML);
        BD = v.findViewById(R.id.BD);
        CC = v.findViewById(R.id.CC);
        IOT = v.findViewById(R.id.IOT);
        CSE = v.findViewById(R.id.CSE);
        contact = v.findViewById(R.id.contact);
        reel = v.findViewById(R.id.reel);
        uid = v.findViewById(R.id.uid);
        rno = v.findViewById(R.id.roomno);
        concern =v.findViewById(R.id.concern);
        raise = v.findViewById(R.id.raise);
        hostel = v.findViewById(R.id.hostel);
        autoCompleteTextViewblock = v.findViewById(R.id.sblock);
        autoCompleteTextViewfloor = v.findViewById(R.id.sfloor);



        //selecting block
        adapterItems = new ArrayAdapter<String>(getActivity(), R.layout.list_item,blocks);
        autoCompleteTextViewblock.setAdapter(adapterItems);
        autoCompleteTextViewblock.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                itemblock = adapterView.getItemAtPosition(i).toString();
//                Toast.makeText(getActivity(), "Block: "+itemblock, Toast.LENGTH_SHORT).show();
            }

        });
        //

        // selecting floors
        adapterItems = new ArrayAdapter<String>(getActivity(), R.layout.list_item,floors);
        autoCompleteTextViewfloor.setAdapter(adapterItems);
        autoCompleteTextViewfloor.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                itemfloor = adapterView.getItemAtPosition(i).toString();
//                Toast.makeText(getActivity(), "Floor: "+itemfloor, Toast.LENGTH_SHORT).show();
            }

        });
        //


        //open cuims
        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goLink("https://uims.cuchd.in/uims/");
            }
        });

        //hosetel
        hostel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), hostel.class));
                getActivity().finish();
            }
        });


        //issue raiser
        raise.setEnabled(isNetworkAvailable());

        if(isNetworkAvailable()){
            raise.setEnabled(true);
//            raise.setText("Raise Issue");
        }
        else{
            raise.setEnabled(false);
            raise.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
            raise.setTextColor(Color.BLACK);
            raise.setText("Check Connection");
            raise.setTextSize(18);
        }


        raise.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                username = "my_email";
                password = "my_password";
                String messageToSendone = itemblock;
                String messageToSendtwo = itemfloor;
                String messageToSendthree = rno.getText().toString();
                String messageToSendfour = concern.getText().toString();

                StringBuilder all = new StringBuilder();
                        all.append(messageToSendone);
                        all.append(messageToSendtwo);
                        all.append(messageToSendthree+" is facing issue of:  ");
                        all.append(messageToSendfour);
                        messageToSend = all.toString();


                subject = uid.getText().toString();

                if (TextUtils.isEmpty(subject)){
                    uid.setError("Please enter your registered CU mail ID!");
                    return;
                }
//                if (!subject.contains("@cuchd.in") && !subject.contains("@cumail.in")
//                    && !subject.contains("@CUCHD.IN") && !subject.contains("@CUMAIL.IN")){
//                    uid.setError("Please enter correct CU mail ID!");
//                    return;
//                }
                if (TextUtils.isEmpty(messageToSendone)){
                    autoCompleteTextViewblock.setError("Please select the block no.");
                    return;
                }
                if (TextUtils.isEmpty(messageToSendtwo)){
                    autoCompleteTextViewfloor.setError("Please select the floor!");
                    return;
                }

                if (TextUtils.isEmpty(messageToSendthree)){
                    rno.setError("Please enter your room no.!");
                    return;
                }
                if (messageToSendthree.length() < 3) {
                    rno.setError("Please enter valid room no.!");
                    return;
                }
                if (messageToSendthree.length() > 4) {
                    rno.setError("Please enter valid room no.!");
                    return;
                }

                if (TextUtils.isEmpty(messageToSendfour)){
                    concern.setError("Please enter your issue!");
                    return;
                }

                //Caling the Asynctask class to execute and send mail
                    mytask = new Mytask().execute();

            }
        });



        //stay nerd card
        webView = v.findViewById(R.id.wb);
        webView.loadUrl("https://sites.google.com/d/1r16RJHz0_2FwcYyP5V0pnrr_3pombkrA/p/1T5T_7ckEO9cSJK2TnV41PNBJXQlHzp3x/edit");
        webView.setBackgroundColor(0);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }
        });



        //INTERNET ERROR MSG
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/nerd.html");

            }
        });




        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);





        //shottbyparth
        webView2 = v.findViewById(R.id.wb2);
        webView2.loadUrl("https://sites.google.com/view/shottbyparth");
        webView2.setBackgroundColor(0);

        //keep in app
        webView2.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }
        });



        //INTERNET ERROR MSG
        webView2.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/partherror.html");

            }
        });




        WebSettings webSettings2 = webView2.getSettings();
        webSettings2.setJavaScriptEnabled(true);
        webView2.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView2.getSettings().setAllowFileAccess(true);


        //magic happens compiler
        webView1 = v.findViewById(R.id.wb1);


        webView1.loadUrl("https://www.tutorialspoint.com/online_cpp_compiler.php");
        webView1.setBackgroundColor(0);

        //keep in app
        webView1.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }

        });


        //back button
        webView1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if(keyEvent.getAction() == keyEvent.ACTION_DOWN){
                    if(keyCode == KeyEvent.KEYCODE_BACK){
                        if(webView1 != null){
                            if(webView1.canGoBack()){
                                webView1.goBack();
                            }else {
                                getActivity().onBackPressed();
                            }

                        }
                    }
                }
                return true;
            }
        });




        webView1.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/compiler.html");

            }
        });

        //hiding web element with error message

        webView1.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                view.loadUrl("javascript:(function() { " +
                        "document.getElementById('north').style.display='none';})()");
            }
        });






        WebSettings webSettings1 = webView1.getSettings();
        webSettings1.setJavaScriptEnabled(true);





        webView1.setVerticalScrollBarEnabled(true);
        webView1.setHorizontalScrollBarEnabled(true);
        webView1.setScrollbarFadingEnabled(false);
        webView1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
//        webView1.requestFocus();








        //reel
        reel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goLink("https://sites.google.com/view/shottbyparth");

            }
        });







        //notes
        AIML.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goLink("https://drive.google.com/drive/folders/1bolCGvvV9dKUFH8Uooe3DQGsP4gL1Vwu?usp=share_link");

            }
        });

        BD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Content coming soon...", Toast.LENGTH_SHORT).show();
            }
        });

        CC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Content coming soon...", Toast.LENGTH_SHORT).show();
            }
        });

        IOT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Content coming soon...", Toast.LENGTH_SHORT).show();
            }
        });

        CSE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Content coming soon...", Toast.LENGTH_SHORT).show();
            }
        });


        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goLink("https://uims.cuchd.in/uims/");
            }
        });

        return v;
    }



    //asynctask class
    public class Mytask extends AsyncTask<Void, Void, Exception>
    {
        ProgressDialog dialogue;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialogue = new ProgressDialog(getActivity());
            dialogue.setTitle("Processing");
            dialogue.setMessage("Calm down, your issue is being raised...");
            dialogue.setIndeterminate(true);
            dialogue.setCancelable(false);
            dialogue.show();

        }
        @Override
        protected Exception doInBackground(Void... params) {

            ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


            Properties properties = new Properties();
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.socketFactory.port", "587");
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            session = Session.getInstance(properties,
                    new javax.mail.Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });





                try {
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress(username));
                        message.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse("[email protected]"));
                        message.setSubject(subject);
                        message.setText(messageToSend);
                        Transport.send(message);;


                } catch (MessagingException e) {
                    Log.e("EmailSendingError", e.getMessage());
                    throw new RuntimeException(e);

                }


//            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//            StrictMode.setThreadPolicy(policy);
            return null;
        }
        @Override
        protected void onPostExecute(Exception e) {

//            if (e != null) {
//
//                Toast.makeText(getActivity(), "Error: " + e, Toast.LENGTH_LONG).show();

//            } else {

//            }

            if (e == null) {
                if(dialogue.isShowing()){
                    dialogue.dismiss();
                }
                rno.getText().clear();
                concern.getText().clear();
                uid.getText().clear();
                autoCompleteTextViewblock.getText().clear();
                autoCompleteTextViewfloor.getText().clear();

                Toast.makeText(getActivity(), "Concern Raised Successfully", Toast.LENGTH_LONG).show();
            } else {
                // Handle failure...
                Toast.makeText(getActivity(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }



    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }





    private void goLink(String s) {
        Uri uri = Uri.parse(s);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }



}```

I tried to check the internet before sending the mail, but it is not handling the problem when internet turns suddenly off while sending the mail. So I need the solution for this problem!
android-asynctask jakarta-mail

评论

0赞 Mohd Asif Ahmed 2/16/2023
同时粘贴崩溃日志

答: 暂无答案