After sending sms, android activity is automatically moved to the same activity

I have created two activity pages. MainActivity and Doceed Activity. I was getting data from the user on the MainActivity page and through the intent I was sending to continue working.

Continue the page, I entered a phone number and then I pressed the submit button. it was posting successfully, but after that I started with start to go to mainactivity page. it goes to the main activity, but after a few seconds it automatically takes over.

1. In mainActivity, I was sending a list like

public void Proceed(View view){
        if(planetsList.size()==0){

            Toast.makeText(MainActivity.this, "Please Add Product", Toast.LENGTH_SHORT).show();
            return;

        }
        Intent intent = new Intent(MainActivity.this, Proceed.class);
        intent.putExtra("list_product", planetsList);
        Log.e("gjdgfl",Integer.toString(planetsList.size()));
        startActivity(intent);

    }

      

2. On the "Continued" page Encoding

send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String no = mnumber.getText().toString();
                Float netTotal = 0.1f;

                ArrayList<Planet> myList = getIntent().getParcelableArrayListExtra("list_product");

                Log.e("hai1",Integer.toString(myList.size()));
                StringBuilder sb = new StringBuilder();
               for (Planet temp : myList) {
                    sb.append(temp.getName()).append(" ").append(temp.getPerkg()).append("*").append(temp.getTotkg()).append("=").append(temp.getTotamt()).append("\n");
                   netTotal += temp.getTotamt();
                }
                sb.append("Total Amount is -"+netTotal);
                System.out.println(sb.toString());
                System.out.println(no);
                String msg = sb.toString();

                Intent intent=new Intent(getApplicationContext(),Proceed.class);
                PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
                SmsManager sms=SmsManager.getDefault();
                sms.sendTextMessage(no, null, msg, pi,null);
                Toast.makeText(getApplicationContext(), "Message Sent successfully!", Toast.LENGTH_LONG).show();



                AlertDialog.Builder builder = new AlertDialog.Builder(Proceed.this);
                builder.setTitle("Confirmation");
                builder.setMessage("Message Sent Successfully")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                               Intent intent1 = new Intent(Proceed.this,MainActivity.class);
                                intent1.putExtra("exit_on_sent", true);
                                startActivity(intent1);

                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();

}

      

+3


source to share


3 answers


Change the PendingIntent

class to MainActivity

, it will redirect MainActivity after sending sms.



Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
SmsManager sms=SmsManager.getDefault();

      

0


source


Read it:

If you give an intent to a foreign application and this application sends / broadcasts the intent you gave, they will execute the intent with their own permissions. But if you instead use a PendingIntent application that you created using your own permission, that application will execute the contained intent using the application permission.

here is the problem:

You dispatch the intent by going to Proceed.class awaiting the intent this way when the message is complete it goes to Proceed.class



      Intent intent=new Intent(getApplicationContext(),Proceed.class);
                    PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
                    SmsManager sms=SmsManager.getDefault();

      

Change this line:

Intent intent=new Intent(getApplicationContext(),MainActivity.class);

      

+2


source


Pending intent

 PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);

      

will wait for the event and when the message is done you tell it to go to the Proceed class (you gave it in intent)

 Intent intent=new Intent(getApplicationContext(),Proceed.class);

      

After the messaging is complete, an alert dialog will appear and the confirmation button will navigate to MainActivity. The problem here is the PendingIntent is still active and your app will still receive it until you cancel it, or make sure you can only use it once.

1. Cancel the pending intent

 Intent intent=new Intent(getApplicationContext(),Proceed.class);
 PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
 alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
 alarmManager.cancel(pi);

      

2. Use the pending hint only once

PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,PendingIntent.FLAG_ONE_SHOT);

      

+1


source







All Articles