如何创建警报对话框时,我收到短信?

5t7ly7z5  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(214)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

5天前关门了。
改进这个问题
我有一个跟踪应用程序,我想替换if else to alert对话框中的toast消息。

public class ReceiveSms extends BroadcastReceiver {
    String BalanceMessage, BalanceMonth, BalanceDay, BalanceYear;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();
            SmsMessage[] messages;
            String PhoneNumber;
            if(bundle != null){
                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    messages = new SmsMessage[pdus.length];
                    String format = bundle.getString("format");
                    for (int i = 0; i < messages.length; i++){
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                        }
                        else {
                            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        }
                        PhoneNumber = messages[i].getOriginatingAddress();
                        String MessageBody = messages[i].getMessageBody();
                        if(PhoneNumber.equals(GlobalVariable.AccountNumber)){
                            if(MessageBody.contains("maps.google.com")){
                                int indexStart = MessageBody.indexOf("q=loc:") + 6;
                                int indexCount = 0;
                                MainActivity.staticOpenMapButton.setVisibility(View.VISIBLE);
                                for (int a = indexStart; a <MessageBody.length() ; a++) {
                                    indexCount++;
                                }
                                int indexEnd = indexStart + indexCount - 1;
                                String message = MessageBody.substring(indexStart,indexEnd);
                                String MessageSplit [] = message.split(",");
                                Double latitude = Double.parseDouble(MessageSplit[0]);
                                Double longitude = Double.parseDouble(MessageSplit[1]);
                                GlobalVariable.Latitude = latitude;
                                GlobalVariable.Longitude = longitude;
                            }else if(MessageBody.contains("Login Successfully")){
                                String MessageSplit [] = MessageBody.split(",");
                                Intent OpenMainActivity = new Intent(context,MainActivity.class);
                                OpenMainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(OpenMainActivity);
                                Toast.makeText(context, "Login Successfully", Toast.LENGTH_SHORT).show();
                                if(MessageSplit[1].equals("+63" + LogIn.Number.getText().toString())){
                                    GlobalVariable.SecondAccountNumber = "";
                                    GlobalVariable.ThirdAccountNumber = "";
                                    GlobalVariable.SecondAccountNumber = MessageSplit[2];
                                    GlobalVariable.ThirdAccountNumber = MessageSplit[3];
                                }else if(MessageSplit[2].equals("+63" + LogIn.Number.getText().toString())){
                                    GlobalVariable.SecondAccountNumber = "";
                                    GlobalVariable.ThirdAccountNumber = "";
                                    GlobalVariable.SecondAccountNumber = MessageSplit[1];
                                    GlobalVariable.ThirdAccountNumber = MessageSplit[3];
                                }else if(MessageSplit[3].equals("+63" + LogIn.Number.getText().toString())){
                                    GlobalVariable.SecondAccountNumber = "";
                                    GlobalVariable.ThirdAccountNumber = "";
                                    GlobalVariable.SecondAccountNumber = MessageSplit[1];
                                    GlobalVariable.ThirdAccountNumber = MessageSplit[2];
                                }
                                Toast.makeText(context, MessageSplit[1], Toast.LENGTH_SHORT).show();
                                Toast.makeText(context, MessageSplit[2], Toast.LENGTH_SHORT).show();
                                Toast.makeText(context, MessageSplit[3], Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Incorrect Password")){
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle("Information");
                                builder.setMessage("Incorrect Password");
                                builder.create().show();
                                Toast.makeText(context, "Incorrect Password", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Account Number is not recognized by device.")){
                                Toast.makeText(context, "Account Number is not recognized by device. Please check the number", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Account Number Successfully Added")){
                                Toast.makeText(context, "Account Number Successfully Added", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Number Already Registered")){
                                Toast.makeText(context, "Number Already Registered", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("User Account is Full")){
                                Toast.makeText(context, "User Account is Full. Maximum User is 3 only", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("User Account Deleted")){
                                Toast.makeText(context, "User Account Successfully Deleted", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("User Password is Incorrect")){
                                Toast.makeText(context, "User Password is Incorrect", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Account Number Deletion Failed")){
                                Toast.makeText(context, "Account Number Deletion Failed", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Password Successfully Changed")){
                                Toast.makeText(context, "Password Successfully Changed", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Account Number is not Registered")){
                                Toast.makeText(context, "Account Number is not Registered", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Regular Load")){
                                if(MessageBody.contains("Unli Allnet Texts")){
                                    int indexStart = MessageBody.indexOf("Unli Allnet Texts") + 18;
                                    BalanceMessage = MessageBody.substring(indexStart);
                                    BalanceMonth = BalanceMessage.substring(7,10);
                                    BalanceDay = BalanceMessage.substring(5,7);
                                    BalanceYear = BalanceMessage.substring(10,22);
                                    GlobalVariable.TextBalance = "Unlimited";
                                    DeviceBalance.staticTextBalance.setText("Unlimited");
                                    BalanceExpiryDate();
                                }else if(MessageBody.contains("Allnet Texts:")){
                                    int indexStart = MessageBody.indexOf("Allnet Texts:") + 13;
                                    BalanceMessage = MessageBody.substring(indexStart);
                                    int indexExpiry = BalanceMessage.indexOf("Exp:");
                                    String AvailableText = BalanceMessage.substring(0, indexExpiry);
                                    BalanceMonth = BalanceMessage.substring(indexExpiry + 7,indexExpiry + 10);
                                    BalanceDay = BalanceMessage.substring(indexExpiry + 5,indexExpiry + 7);
                                    BalanceYear = BalanceMessage.substring(indexExpiry + 10,indexExpiry + 22);
                                    GlobalVariable.TextBalance = AvailableText;
                                    DeviceBalance.staticTextBalance.setText(AvailableText);
                                    BalanceExpiryDate();
                                }else if(MessageBody.contains("FREE Allnet Texts")){
                                    int indexStart = MessageBody.indexOf("Allnet Texts") + 12;
                                    BalanceMessage = MessageBody.substring(indexStart);
                                    int indexExpiry = BalanceMessage.indexOf("Exp:");
                                    String AvailableText = BalanceMessage.substring(0,indexExpiry);
                                    Toast.makeText(context, AvailableText,Toast.LENGTH_SHORT).show();
                                    BalanceMonth = BalanceMessage.substring(indexExpiry + 7,indexExpiry + 10);
                                    BalanceDay = BalanceMessage.substring(indexExpiry + 5,indexExpiry + 7);
                                    BalanceYear = BalanceMessage.substring(indexExpiry + 10,indexExpiry + 22);
                                    GlobalVariable.TextBalance = AvailableText;
                                    DeviceBalance.staticTextBalance.setText(AvailableText);
                                    BalanceExpiryDate();
                                }
                            }else{
                                Toast.makeText(context, "No location found! Please Try Again.", Toast.LENGTH_SHORT).show();
                            }
                        }else if(PhoneNumber.equals("+63" + Register.contact.getText())){
                            if(MessageBody.contains("Register Successfully")){
                                Toast.makeText(context, "Register Success. Please Sign In Now", Toast.LENGTH_SHORT).show();
                            }else if(MessageBody.contains("Motorcycle Already Registered")){
                                Toast.makeText(context, "Motorcycle Already Registered", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(context, "Failed to Register. Please try again!.", Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(context, "Message receive from different number", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    private void BalanceExpiryDate(){
        if(BalanceMessage.contains("Jan")){
            GlobalVariable.BalanceExpiry = "01/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("04/" + BalanceDay + "/" + BalanceYear);
        } else if(BalanceMessage.contains("Feb")){
            GlobalVariable.BalanceExpiry = "02/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("02/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Mar")){
            GlobalVariable.BalanceExpiry = "03/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("03/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Apr")){
            GlobalVariable.BalanceExpiry = "04/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("04/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("May")){
            GlobalVariable.BalanceExpiry = "05/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("05/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Jun")){
            GlobalVariable.BalanceExpiry = "06/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("06/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Jul")){
            GlobalVariable.BalanceExpiry = "07/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("07/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Aug")){
            GlobalVariable.BalanceExpiry = "08/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("08/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Sep")){
            GlobalVariable.BalanceExpiry = "09/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("09/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Oct")){
            GlobalVariable.BalanceExpiry = "10/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("10/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Nov")){
            GlobalVariable.BalanceExpiry = "11/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("11/" + BalanceDay + "/" + BalanceYear);
        }else if(BalanceMessage.contains("Dec")){
            GlobalVariable.BalanceExpiry = "12/" + BalanceDay + "/" + BalanceYear;
            DeviceBalance.staticBalanceExpiry.setText("12/" + BalanceDay + "/" + BalanceYear);
        }
    }

}*

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题