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>