Android:startActivityForResult没有调用onActivityResult

时间:2011-10-27 17:41:29

标签: android ipc android-service android-activity

我的设置

  • 在自己的进程中运行的服务,CentralService
  • 调用startActivityForResult(),MainActivity
  • 的活动
  • 正在为结果启动的活动,ReturnResultActivity

我正在尝试做什么

  1. 启动ReturnResultActivity并将其绑定到服务(注册其处理程序)
  2. 让任何其他活动想要运行
  3. 当它收到来自服务的消息时:
    • 从服务中取消绑定
    • 完成()
    • 的setResult()
      1. 让MainActivity的onActivityResult()方法名为
  4. 使用Log.i我已确认步骤1到3发生了。但是,当应该调用onActivityResult()方法时,我在日志中得到以下内容:

    V/ActivityManager(   60): Finishing activity: token=HistoryRecord{442cc518 com.myActivity/.primitives.ReturnResultActivity}, result=3, data=Intent { (has extras) }
    V/ActivityManager(   60): Adding result to HistoryRecord{442a3988 com.mainActivity.sample/.MainActivity} who=null req=500 res=3 data=Intent { (has extras) }
    

    其他信息

    每个实体都在一个单独的项目中(我有3个项目相互交互),因此它们都在自己的流程中运行。

    MainActivity从服务启动,代码如下:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setClassName(activityInfo.packageName, activityInfo.name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
              | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);
    

    部分代码

    返回结果活动:

    public class ReturnResultActivity extends SomeActivity {
        private static final boolean DEBUG = true;
        private static final String TAG = "ReturnResultActivity";
    
        protected void onBind() {
            // We want to monitor the service for as long as we are
            // connected to it.
            Message msg = Message.obtain(null,
                    CentralService.MSG_LISTEN);
            msg.replyTo = mMessenger;
    
            try {
                mService.send(msg);
            } catch (RemoteException e) {
                // In this case the service has crashed before we could even
                // do anything with it; we can count on soon being
                // disconnected (and then reconnected if it can be restarted)
                // so there is no need to do anything here.
                e.printStackTrace();
            }
        }
    
        /** this method is called eventually after doUnbindService is called **/
        protected void onUnbind() {
            if (DEBUG) Log.i(TAG, "Unbinding and finishing"); //I can tell this gets called when I run it
            if (data != null) {
                setResult(CentralService.MSG_SPEECH_RECOGNIZED, data);
            }
            finish();
        }
    
        Intent data;
        protected boolean receiveMessage(Message msg) {
            if (DEBUG) Log.i(TAG, "Incoming Message to Listener: " + msg.what);
    
            switch (msg.what) {
                case ASRManager.MSG_SPEECH_RECOGNIZED:
                    data = new Intent();
                    Bundle bundle = msg.getData();
                    bundle.setClassLoader(getClassLoader());
                    data.putExtra(ASRManager.TOKEN_PARCEL_KEY, bundle.getParcelable(ASRManager.TOKEN_PARCEL_KEY));
                    if (DEBUG) Log.i(TAG, "Received speech, setting result.");
                    doUnbindService();
    
                    return true;
                default:
                    return false;
            }
        }
    
    
    }
    

    主要活动

    public class MainActivity extends CustomActivity {
        private static final boolean DEBUG = true;
        private static final String TAG = "MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent intent = new Intent("com.custom.returnresultaction");
            startActivityForResult(listenIntent, 500);
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (DEBUG) Log.i(TAG, "Got activity result " + resultCode); //this doesn't get called
    
        }
    }
    

    我的LogCat窗口中没有显示堆栈跟踪。 请帮忙!

    编辑:有趣的新信息。如果我然后从启动器启动MainActivity,我在logcat中看到以下内容:

    I/ActivityManager(   60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mainActivity.sample/.MainActivity }
    V/ActivityManager(   60): Sending result to HistoryRecord{44253568 com.android.launcher/com.android.launcher2.Launcher} (index -1)
    I/ActivityManager(   60): Starting Activity Unchecked Locked
    V/ActivityManager(   60): com.mainActivity.sample.MainActivity Audio Activity Found
    V/ActivityManager(   60): Delivering results to HistoryRecord{442c0310 com.mainActivity.sample/.MainActivity}: [ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}]
    V/ActivityThread(  327): Handling send result to ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainAcitivty.sample.MainActivity}}
    V/ActivityThread(  327): Delivering result to activity ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainActivity.sample.MainActivity}} : ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}
    I/MainActivity(  327): Got activity result 3
    

    我的理论是,运行的任务不包含MainActivity,因为消息是从CentralService接收的。有什么想法吗?知道如何切换到正确的任务。 (注意即使这可能看起来很糟糕,也可能会破坏用户切换任务并将另一个Activity带到最前端,这就是我想要做的。这是因为这最终将在自定义版本的android上运行,这将使所有这不是破坏性的。)

2 个答案:

答案 0 :(得分:3)

2年后回到这个问题后,问题是因为活动是在新任务中启动的。 (我把它设置在我开火的意图中)。为立即启动新任务的结果启动活动将返回-1。

答案 1 :(得分:1)

致电后

startActivityForResult(listenIntent, 500);

在com.custom.returnresultaction类中,您是否包含此代码?

setResult(RESULT_CANCELED, i);

or setResult(RESULT_OK, i);

and call finish();

您需要设置结果并调用finish()。