【问题标题】:Get data from another application in android从android中的另一个应用程序获取数据
【发布时间】:2014-02-21 04:25:26
【问题描述】:

我安装了两个应用程序 app1 和 app2。 我想从 app1 启动 app2 的活动。然后使用来自 app2 的一些数据返回 app1 上的活动。但我可以返回 app1 ,但无法获取数据。

来自 app1:

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, sampleText);
        intent.setType("text/plain");
        intent.setComponent(new ComponentName("com.example.app2","com.example.app2.MainActivity"));     
        startActivity(intent);

来自 app2:

Intent goBack = new Intent();
        goBack.putExtra("result","asdaf");
        setResult(RESULT_OK, goBack);
        finish();

app1 的清单:

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

app2 的清单:

 <intent-filter>
                <action android:name="android.intent.action.MAIN" />                
                <category android:name="android.intent.category.LAUNCHER" />               
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />    
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />    
            </intent-filter>

我在 app1 中的功能没有受到影响。

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

             if(resultCode == RESULT_OK){      
                 String result=data.getStringExtra("result");                   
                TextView tv = (TextView)findViewById(R.id.textView3);
                tv.setText(result);
             }
             if (resultCode == RESULT_CANCELED) {    
                 //Write your code if there's no result
             }
          }
        }

我在这里缺少什么?是否需要为此进行任何设置?

【问题讨论】:

    标签: java android android-intent android-manifest


    【解决方案1】:

    您应该在“app1”中使用 startActivityForResult:

    startActivityForResult(intent, SOME_CONSTANT);
    

    您传递的常量将作为 requestCode 参数传回。因此,在您的情况下,您似乎想要传入 1。

    有关更多信息,请参阅文档:Android Developers

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多