1

I cant solve my problem soo I need help :)

I have two Apps "App A" and "App B". In "App A" im entry two numerbs(in two diffrent edittext) after that i click button "sendwithdata"(here Im passing parameters from edittext using URI). My "App 2" catch parameters from "App A", adds two numbers and displays the result. I thik its something wrong with geting parameters from uri.

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 manifest

<?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>

Michal O
  • 27
  • 7

2 Answers2

1

I ran the code and figured out where the problems were.

The main problem was that you were missing & between the parameters, so that made the first parameter contain everything after liczbaA, and the second parameter was blank.

You also accidentally used liczbaA twice in the adresUriSumy() method.

I switched it to explicitly use Strings for the EditText values, but it would work as you have it as well, since it will implicitly call toString() on each Editable when you use it in String.format().

The last issue was formatting the result in App B.

First, here are the fixes from App A that made it work:

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);
}

Here is the fix made in 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));

    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

I think you should be using putExtra() on the Intent class.

See the docs for an example. Here's another post that shows how to do this.

Community
  • 1
  • 1
Dan Hintz
  • 23
  • 1
  • 4