2

So I'm having trouble trying to pass the greater of two entered numbers from one app and then have the receiver broadcast it in another. Here's code from the sending app:

public class MainActivity extends AppCompatActivity {

    private EditText etFirst;
    private EditText etSecond;
    private Button btSend;
    private int num;


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

        etFirst = (EditText) findViewById(R.id.etFirst);
        etSecond =(EditText) findViewById(R.id.etSecond);
        btSend = (Button) findViewById(R.id.btSend);

        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int a = Integer.parseInt(etFirst.getText().toString());
                int b = Integer.parseInt(etSecond.getText().toString());

                if (a > b) {
                    num = a;
                }
                else {
                    num = b;
                }

                Intent intent = new Intent("com.numb.sending.intent");
                intent.putExtra("com.numb.sending.intent.params", num);
                sendBroadcast(intent);
            }
        });

    }


}

Code from the receiving app:

public class MainActivity extends AppCompatActivity {

    private Button btReceive;

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

        btReceive = (Button) findViewById(R.id.btReceive);

        btReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyReceiver.INTENT);
                intent.putExtra(MyReceiver.PARAM);
                sendBroadcast(intent);
            }
        });
    }
}

My receiver:

public class MyReceiver extends BroadcastReceiver {
    public static final String PARAM = "com.numb.sending.intent.params";
    public static final String INTENT = "com.numb.sending.intent";


    public MyReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.hasExtra(PARAM)) {
            String value = intent.getStringExtra(PARAM);
            Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
        }
    }
}

Any help?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.numb.sending">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.numb.sending.intent"/>
            </intent-filter>
        </receiver>
    </application>

etalo
  • 21
  • 2
  • I'm not sure how you expect `intent.hasExtra(PARAM)` to work when the string for PARAM is different – OneCricketeer Dec 23 '16 at 01:27
  • No, that's not the issue..just didn't change those in the last part before copy/pasting. – etalo Dec 23 '16 at 01:37
  • Can you log any message within `onReceive`? Are you sure the receiver works? – OneCricketeer Dec 23 '16 at 01:55
  • I get an error in the intent.putExtra line..it will work if I add "some string" after PARAM, but then all I'm getting is that string. `btReceive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MyReceiver.INTENT); intent.putExtra(MyReceiver.PARAM); sendBroadcast(intent); }` – etalo Dec 23 '16 at 02:10
  • You have to put some string, though... That's the value of the param – OneCricketeer Dec 23 '16 at 02:12
  • The correct method... `putExtra (String name, CharSequence value)`... It is takes two parameters – OneCricketeer Dec 23 '16 at 02:14
  • Honestly, I do not know why you need a button to receive data. I thought you wanted to send the data from the other app? – OneCricketeer Dec 23 '16 at 02:16
  • I want to send the value of num from the other app to output in this one. It could be with a button or no. – etalo Dec 23 '16 at 02:22
  • Right, so `btReceive` works if you add some string, but that is not the maximum of the two values from the other app, so why are you trying to ask about an error you are getting within `btReceive` click event? In other words, what is the actual issue with `btSend`? – OneCricketeer Dec 23 '16 at 02:26
  • Anyway, please add the manifest from the receiving application to show how you set the receiver – OneCricketeer Dec 23 '16 at 02:28
  • I put the manifest above. I'm a novice at this, but as far as I can tell btSend is doing what is intended, but that value is not being picked up on the other side. Appreciate you trying to help.. – etalo Dec 23 '16 at 02:35
  • As far as I can tell, what you have looks like fine. http://stackoverflow.com/questions/33492790/how-to-send-broadcast-from-one-app-to-another-app – OneCricketeer Dec 23 '16 at 02:38
  • I don't know, can't figure it out. – etalo Dec 23 '16 at 03:02

0 Answers0