android 在 startActivity 意图完成时通知?

2024-01-16

我需要能够判断生成的活动(通过意图)何时完成,我该怎么做?

这就是我所拥有的:

    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

根据下面的建议,我做了一些修改。然而,现在,一旦发送文本,启动的 SMS 活动就“坐在那里”。我不知道如何让它返回调用活动。这就是我所拥有的:

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

做你想做的事,在开始的活动中,当你认为活动已成功完成时,执行setResult(Activity.RESULT_OK); finish();

并且在开始覆盖的活动中onActivityResult()方法。并测试您调用的活动是否完成了工作。

从来没有使用过Activity.RESULT_OK for requestCode,因为它是负常数。文件说onActivityResult()将为每个正整数调用方法requestCode:

重写并遵循这个

如何开始活动以获得结果:

      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您完成的活动的结果如下:

    //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
}

你如何使用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);

    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 在 startActivity 意图完成时通知? 的相关文章

随机推荐