多个onActivityResult {}在同一个类中?

时间:2011-12-07 18:22:03

标签: android android-activity

我正在我的应用上做几个对话框选择器,并在我的onClickListener上使用一个开关案例,但是对话框正在调用onActivityResult{}但是如果我添加2,则应用程序崩溃...可以我把它们放在同一个onActivityResult{}中,通过结果代码调用它们?代码如下:

@Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
            case R.id.bContacts:

                Intent i = new Intent(Intent.ACTION_PICK, 
                        ContactsContract.Contacts.CONTENT_URI); 
                        startActivityForResult(i, PICK_CONTACT); 

                break;  

            case R.id.bRingtone:

                String uri = null;
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
                startActivityForResult( intent, Set_Ringtone);

                break;
        }
    }

 @Override 
        protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) 
        { 
         super.onActivityResult(requestCode, resultCode, data); 
            Cursor c = getContact(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);

            if (c.moveToNext()) {
                String name = c
                        .getString(c
                                .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                callName.setText(name);
                String phoneNumber = c
                        .getString(c
                                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                callNum.setText(phoneNumber);
                Log.d("Cont", "name "+name+" no. " + phoneNumber);
            } 
        } 


  @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
    {
        if (resultCode == Activity.RESULT_OK && requestCode == 5)
        {
             Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

             if (uri != null)
             {

                 String ringTonePath = uri.toString();
                 Toast.makeText(this, "GOT IT" + ringTonePath, Toast.LENGTH_LONG).show();
             }
             else
             {
                 Toast.makeText(this, "DIDNT", Toast.LENGTH_SHORT).show();
             }
         }            
     }

1 个答案:

答案 0 :(得分:7)

您可以使用相同的onActivityResult()方法处理这两种意图。这就是为什么当你致电startActivityForResult()时发送一个带有你意图的请求代码的原因,当结果出来进行区分时你会得到它。您的方法可能如下所示:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) {
        case PICK_CONTACT:
           // handle the contact result
        break;

        case Set_Ringtone:
           // handle the ringtone result
        break;
    }
}

您的应用很有可能崩溃,因为如果您选择了错误的意图,您尝试使用未提供的结果的详细信息(例如,您尝试在此处阅读联系人姓名,则铃声结果将无效)