如何在android中从一个应用程序传输数据到另一个应用程序



所以,我这里需要的是从App1发送数据到App2。我已经尝试了简单的代码,我所做的,但有些东西是错误的。我想在app1中对1 editText进行简单的输入,然后它将被app2接收。

的例子:

App1: EditText ->我输入'Hello!'

然后

App2: TextView -> 'Hello!'

但是接下来发生的是,App2中的textview没有显示任何内容。这是怎么了?

这些是我试过的代码。

My Application 1:

secondAppButton = (Button)findViewById(R.id.secondAppButton);
    namaEditText = (EditText)findViewById(R.id.namaEditText);
    secondAppButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nama = namaEditText.getText().toString();
            Intent secondApp = getPackageManager().getLaunchIntentForPackage("com.example.yosua.myapplication2");
            Bundle bundleApp = new Bundle();
            bundleApp.putString("namaOrang", nama);
            secondApp.putExtras(bundleApp);
            startActivity(secondApp);
        }
    });

My Application 2:

namaDariApp1 = (TextView)findViewById(R.id.namaApp2);
    String nama;
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    nama = extras.getString("namaOrang");
    namaDariApp1.setText(nama);

尝试包含键

secondApp.putExtras("key",bundleApp);

然后像这样检索

Bundle extras = intent.getBundleExtra("key");

你必须选择在两个应用程序之间传递一个值,第一个是通过使用ContentProviders是一个很好的方法来在应用程序之间共享数据,或者你可以使用SharedPreferences,就像这个答案

最新更新