如何将数据从广播传递到服务?

时间:2011-12-15 15:26:23

标签: android service android-intent broadcastreceiver

大家好,任何人都可以告诉我如何将数据从广播发送到服务以及如何在服务类中接收这些数据,因为它不能与我合作shuld我收到它((onStartCommand))或((onStart) ))或内部((onReceive)) 以及我应该在Maninafest做什么

String data="hello";
Intent intent1=new Intent(context,Myservice.class);
    intent1.putExtra("Data",data);
    context.startService(intent1);

//when i receive data from broad cast

public int onStartCommand(Intent intent, int flags, int startId) {
String data="";
data=intent.getStringExtra("Phone");
Toast.makeText(this,"here is your data"+data, Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:2)

替换 data=intent.getStringExtra("Phone");data=intent.getStringExtra("Data");