使用来自" App A"的URI在Intentw中传递参数到" App B" -Android

时间:2015-05-14 19:16:58

标签: java android uri

我无法解决我的问题,所以我需要帮助:)

我有两个应用程序" App A"和"应用程序B"。在" App A"我输入两个numerbs(在两个不同的edittext中)之后,我点击按钮" sendwithdata"(这里我使用URI从edittext传递参数)。我的应用2"从" App A"中捕获参数,添加两个数字并显示结果。 我从uri的geting参数中弄错了。

App A

public class WprowadzanieLiczbActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wprowadzanie_liczb);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_wprowadzanie_liczb, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void sendwithData(View view) {
    String adres2 = adresUriSumy();
    Uri uri = Uri.parse(adres2);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

private String adresUriSumy() {
    EditText liczbaAInput = (EditText) findViewById(R.id.A);
    Editable liczbaA = liczbaAInput.getText();
    String liczbaAParam = String.format("liczbaA=%s", liczbaA);

    EditText liczbaBInput = (EditText) findViewById(R.id.B);
    Editable liczbaB = liczbaBInput.getText();
    String liczbaBParam = String.format("liczbaA=%s", liczbaB);

    String podstawowyAdres = "calculator://wwsis.com.pl/suma";

    String adres2 = String.format("%s?%s%s", podstawowyAdres, liczbaAParam, liczbaBParam);

    return (adres2);
}}

App B

public class WyswietlanieActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wyswietlanie);
    wezDanezUri();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_wyswietlanie, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private void wezDanezUri() {
    Uri uri = getIntent().getData();
    if (uri != null) {
        double liczbaA = getDoubleParam(uri, "liczbaA");
        double liczbaB = getDoubleParam(uri, "liczbaB");

        double suma = liczbaA + liczbaB;

        TextView textView = (TextView) findViewById(R.id.wyswietlacz);


        textView.setText(liczbaA + liczbaB + Double.toString(suma));

    }


}

private double getDoubleParam(Uri uri, String queryParamName) {
    String wzorzmiennej = uri.getQueryParameter(queryParamName);
    double value = 0.0;
    try {
        value = Double.parseDouble(wzorzmiennej);
    } catch (Exception e) {
    } // NumberFormatException or NullPointerException
    return (value);
}}

App B清单

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".WyswietlanieActivity"
        android:label="@string/app_name" >
        <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.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="calculator" android:host="wwsis.com.pl" android:path="/suma"/>
        </intent-filter>
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

我运行代码并找出问题所在。

主要问题是您在参数之间缺少&,因此第一个参数包含liczbaA之后的所有内容,第二个参数为空白。

您还在liczbaA方法中意外使用了adresUriSumy()两次。

我将其切换为明确使用字符串作为EditText值,但它也可以正常使用,因为当您使用它时,它会隐式调用每个toString()上的Editable它在String.format()

最后一个问题是在App B中格式化结果。

首先,以下是App A中的修复程序:

private String adresUriSumy() {
    //Use Strings from the EditText fields:
    EditText liczbaAInput = (EditText) findViewById(R.id.A);
    String liczbaA = liczbaAInput.getText().toString();
    String liczbaAParam = String.format("liczbaA=%s", liczbaA);

    //Use liczbaB below:
    EditText liczbaBInput = (EditText) findViewById(R.id.B);
    String liczbaB = liczbaBInput.getText().toString();
    String liczbaBParam = String.format("liczbaB=%s", liczbaB);//modified

    String podstawowyAdres = "calculator://wwsis.com.pl/suma";

    //notice the & below
    String adres2 = String.format("%s?%s&%s", podstawowyAdres, liczbaAParam, liczbaBParam);

    return (adres2);
}

以下是App B中的修复:

private void wezDanezUri() {
    Uri uri = getIntent().getData();

    if (uri != null) {

        double liczbaA = getDoubleParam(uri, "liczbaA");
        double liczbaB = getDoubleParam(uri, "liczbaB");

        double suma = liczbaA + liczbaB;

        TextView textView = (TextView) findViewById(R.id.wyswietlacz);

        //modified:
        textView.setText(Double.toString(liczbaA) + " + " + Double.toString(liczbaB) + " = " + Double.toString(suma));

    }
}

答案 1 :(得分:0)

我认为你应该在Intent类上使用putExtra()。

有关示例,请参阅the docs。这里显示了如何执行此操作的another post