2010-11-23 113 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); 
} 
相關問題