我有一个字符串想要传递给绑定到此服务的主要活动。数据似乎并没有传递到另一端,我已经尝试了多种技术。任何帮助,将不胜感激 这是服务
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");
}
这是活动本身
@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();
}
});
}
答案 0 :(得分:0)
我试图在您的代码中添加一个BroadcastReceiver(不是很漂亮),我评论了添加的行。我可能错过了一些东西或犯了一个错误。随时询问您是否遇到任何错误或阅读https://developer.android.com/reference/android/content/BroadcastReceiver
祝你好运!
您的服务
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);
您的活动
@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();
}
});
}