简体   繁体   中英

After sending sms android activity is Automatically moving to same Activity

i have created two activity pages. MainActivity and Proceed Activity. i was getting data from user in MainActivity page and through intent i was sendging to proceed activity.

Proceed page i was entering phone number then i was clicking send button. it was sending message successfully but after that i was creating start activity to move to mainactivity page. it is moving to main activity but after few seconds it is automatically coming to proceed activity.

1.In mainActivity i was sending 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.in proceed page Coding is

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();

}

Read this :

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

here is the problem :

You are sending the intent as go to Proceed.class in Pending intent thus when message is done 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);

The Pending intent

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

will wait for the event to occur and when messaging is done you are telling it to go to the Proceed class ( you gave it in the Intent)

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

And after the messaging is done,The alert dialog will be shown and the Confirm button takes you to the MainActivity. The problem here is the PendingIntent is still active and your application will still receive it until you cancel it or make sure you can use it only once.

1.Cancel a pending intent by

 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 a pending Intent only once by

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

Change PendingIntent class to MainActivity , it will redirect to MainActivity after sending sms.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM