I have a string that I'm trying to pass to my main activity that is bound to this service. The data doesn't seem to be making it through to the other side and I've tried a plethora of techniques. Any help would be appreciated Here's the service
public int onStartCommand(Intent intent, int flags, int startId) {
String thename=intent.getStringExtra("stockName");
String TAG="hello";
Intent putIntent=new Intent(LocalService.this,Binding.class);
if(thename.equals("AMZN"))
{
//Toast.makeText(this, "The price is $1,755.25", Toast.LENGTH_LONG).show();
putIntent.putExtra("theName","WORKED");
}
and here is the activity itself
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_binding);
final Button priceButton=findViewById(R.id.priceButton);
final EditText stockPrice=findViewById(R.id.stockText);
final Intent theIntent=new Intent(Binding.this,LocalService.class);
priceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stockText=stockPrice.getText().toString();
theIntent.putExtra("stockName",stockText);
startService(theIntent);
Intent getit=getIntent();
String name=getit.getParcelableExtra("theName");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
});
}
I tried adding a BroadcastReceiver to your code (it's not that pretty), I commented the lines I added. It's possible that I've missed something or made an error. Feel free to ask if your get any errors or read https://developer.android.com/reference/android/content/BroadcastReceiver
Good luck!
Your service
public int onStartCommand(Intent intent, int flags, int startId) {
//String for the BroadcastReceiver to listen for.
public static final String BROADCAST_STRING = "your.package.or.whatever.you.want.sending";
String thename=intent.getStringExtra("stockName");
String TAG="hello";
Intent putIntent=new Intent(BROADCAST_STRING);
if(thename.equals("AMZN"))
{
//Toast.makeText(this, "The price is $1,755.25", Toast.LENGTH_LONG).show();
putIntent.putExtra("theName","WORKED");
}
//Broadcast sent and picked up in Activity.
sendBroadcast(putIntent);
Your Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_binding);
final Button priceButton=findViewById(R.id.priceButton);
final EditText stockPrice=findViewById(R.id.stockText);
private BroadcastReceiver broadcastReceiver;
final Intent theIntent=new Intent(Binding.this,LocalService.class);
//Adds a filter to the broadcastReceiver, what it should listen for
IntentFilter filter = new IntentFilter();
filter.addAction("your.package.or.whatever.you.want.sending");
//Triggered when a broadcast is picked up
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Get intent and do what you want
};
// Register the broadcastReceiver and filter
this.registerReceiver(broadcastReceiver, filter);
priceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stockText=stockPrice.getText().toString();
theIntent.putExtra("stockName",stockText);
startService(theIntent);
Intent getit=getIntent();
String name=getit.getParcelableExtra("theName");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.