开发者

android notify when startActivity intent has completed?

I need to be able to tell when a spawned activity (via an intent) has completed, how would I do so?

This is what I have:

    alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String uri = "smsto:" + "";
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
            intent.putExtra("sms_body", PASSWORD_GENERATOR
                    .generatePasswordForSeed(seedText, hourToUse));
            intent.putExtra("compose_mode", true);

            // -- open the text message activity
            startActivity(intent);

            // -- I need to reset the calling activity now, but AFTER the text message activity has completed. Right now the SMS closes right away as I have no wait in...
            finish();
            startActivity(getIntent());
        }
    });

EDIT #1

Per the suggestions below, I've made some modifications. Now, however, the laun开发者_运维知识库ched SMS activity just "sits there" once the text is sent. I can't figure out how to get it to return to the calling activity. This is what I have:

alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String uri = "smsto:" + "";
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
                intent.putExtra("sms_body", PASSWORD_GENERATOR
                        .generatePasswordForSeed(seedText, hourToUse));
                intent.putExtra("compose_mode", true);

                startActivityForResult(intent, Activity.RESULT_OK);

                registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        finish();
                        startActivity(getIntent());
                    }

                }, new IntentFilter("SMS_SENT"));

                ContentResolver contentResolver = getContentResolver();
                Handler handler = new Handler();

                contentResolver.registerContentObserver(Uri
                        .parse("content://sms"), true, new ContentObserver(
                        handler) {

                    @Override
                    public boolean deliverSelfNotifications() {
                        setResult(Activity.RESULT_OK);
                        finish();

                        return super.deliverSelfNotifications();
                    }

                    @Override
                    public void onChange(boolean selfChange) {
                        super.onChange(selfChange);

                        setResult(Activity.RESULT_OK);
                        finish();
                    }
                });
            }
        });

        alertDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        finish();
        startActivity(getIntent());
    }


Use startActivityForResult(intent, positiveinteger);

Do what you want to do, in the started activity when you think activity has finished successfully, do setResult(Activity.RESULT_OK); finish();

And in the activity that started override onActivityResult() method. And test if your called activity finished it job.

Never ever ever ever use Activity.RESULT_OK for requestCode, because it is negative constant. And the Docs says that onActivityResult() method will be called for every positive integer requestCode:

Rewrite and follow this

How you start the activity for result:

      static final int STATIC_RESULT=2; //positive > 0 integer.
      Intent i = new Intent("my.activity.startNewOne");
      i.putExtra("category", category);
    startActivityForResult(i, STATIC_RESULT);

In your startNewOne activity you finish with result like:

    //after the job is done, use the method i mentioned in the comments to check if the sms is delivered/submitted. and proceed if true with this
    if(smsObject.getStatus()==0)//by the docs means successfuly sent
{
    Intent i = getIntent(); //get the intent that has been called, i.e you did called with startActivityForResult();
    i.putExtras(b);//put some data, in a bundle
    setResult(Activity.RESULT_OK, i);  //now you can use Activity.RESULT_OK, its irrelevant whats the resultCode    
    finish(); //finish the startNewOne activity
}

How do you use onActivityResult() method:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          if (requestCode == STATIC_RESULT) //check if the request code is the one you've sent
          {
                 if (resultCode == Activity.RESULT_OK) 
                 {
                // this is successful mission, do with it.

                 {
                 } else
                         // the result code is different from the one you've finished with, do something else.
                     }
          }


        super.onActivityResult(requestCode, resultCode, data);

    }


Followup Answer for second question: Out of curiosity, have you considered sending the text without creating an activity? The reason I ask is because it seems, from what I can tell, no user experience is happening within your SMS activity. If you don't have views and user interactions then maybe just defining a thread or creating a helper class would do the job.

Original Answer for first question: Try using startActivityForResult(Intent, int). Your current activity can get notified when the text message activity finishes.


Unfortunately you will have to use SmsManager

Use this: SMS Messaging in Android | mobiForge

You can receive the SMS_SENT

Note: Make sure you are restarting your calling activity using onCreate()


To answer your new question, you can add exit_on_sent to the intent, which will cause the SMS activity to return after sending an invitation.

smsIntent.putExtra("exit_on_sent", true);

Unfortunately, it looks like it still returns a resultCode of Activity.RESULT_CANCELLED, whether you do it this way or if they hit the back button, so you can't tell if the user has actually sent the text or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜