2010-11-23 87 views
0

我有两个服务: 数据提供者和接收者。将数据从服务传递到另一个服务

我试着做这样:

提供商:

Intent i1 = new Intent(feasibilityEngine.this, SOSFeeder.class); 
i1.putExtra(SENSOR_STRING, "f[i]"); 
startService(i1); 

接收机

Intent intent = getIntent(); 
Bundle b = new Bundle(); 
int i = b.getInt(SENSRO_STRING); 

,但我不能使用getIntent()。

有人可以帮助我吗? TNKS

回答

0

您可以检索作为SENSRO_STRING的价值:

Bundle b = getIntent().getExtras(); 
int i = b.getInt(SENSRO_STRING); 

如果你是在例如广播接收器,在覆盖onReceived方法,您可以拨打:

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Bundle b = intent.getExtras(); 
    int i = b.getInt(SENSRO_STRING); 
0

无需要调用getInent(),你的意图将被传递给onStartCommand()中的接收者服务,这将成为startService()调用的入口点。

示例代码修改自here

接收器服务:

// This is the old onStart method that will be called on the pre-2.0 
// platform. On 2.0 or later we override onStartCommand() so this 
// method will not be called. 
@Override 
public void onStart(Intent intent, int startId) { 
    handleCommand(intent); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    handleCommand(intent); 
    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    return START_STICKY; 
} 

private void handleCommand(Intent intent) { 
    // should this be getStringExtra instead? 
    int i = intent.getIntExtra(SENSRO_STRING, -1); 
} 
相关问题