2012-07-30 50 views
0

'下面是我的兩個類,我試圖將數據從我的活動傳遞到我的服務。我已經傳遞給其他活動沒有問題,但是當我在IntentService中查找我的額外內容時,它總是返回null。任何想法我做錯了嗎?將數據從活動傳遞到服務

public class TestActivity extends Activity { 
private Button mbutton; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mbutton = (Button) this.findViewById(R.id.button1); 
    mbutton.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Log.d("TestActivity", "onClick: starting srvice"); 
      Intent intent = new Intent(TestActivity.this/*getApplicationContext()*/, MyService.class); 
      /*TestActivity.this.getApplicationContext().*/ 
      intent.putExtra(MyService.PARAM_IN_NAME, ((EditText)findViewById(R.id.editText1)).getText()); 
      intent.putExtra(MyService.PARAM_IN_JOB, ((EditText)findViewById(R.id.editText2)).getText()); 
      intent.putExtra(MyService.PARAM_IN_BDAY, ((EditText)findViewById(R.id.editText3)).getText()); 
      startService(intent); 
     } 
    }); 
    } 
} 

這是我的服務類,我試圖從活動中提取我的數據。

public class MyService extends IntentService { 

public static final String PARAM_IN_NAME = "name"; 
public static final String PARAM_IN_JOB = "job"; 
public static final String PARAM_IN_BDAY = "bday"; 

private String mJob; 
private String mBday; 
private String mName; 

// --------------CONSTRUCTORS-------------- 
public MyService() { 
    super("MyServiceThread"); 
} 

public MyService(String name) { 
    super(name); 
} 

// -------------OVERRIDE------------- 
@Override 
public IBinder onBind(Intent arg0) { 
    return null; 
} 

@Override 
public void onDestroy(){ 
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 

    mName = intent.getStringExtra(PARAM_IN_NAME); 
    mJob = intent.getStringExtra(PARAM_IN_JOB); 
    mBday = intent.getStringExtra(PARAM_IN_BDAY); 

    String result = "Name = " + mBday + " | Job = " + mJob + " | Bday = " + mBday; 

    Toast.makeText(this, result, Toast.LENGTH_SHORT).show(); 
} 

}

這也是我有我的清單文件:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".TestActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".MyService" android:enabled="true" /> 
+0

其錯誤你長了 – Khan 2012-07-30 05:27:53

回答

1

您的代碼看起來很不錯。加入toString()後,當您從EditText這樣得到的文本

嘗試:

intent.putExtra(MyService.PARAM_IN_NAME,((EditText)findViewById(R.id.editText1)).getText().toString());

+0

謝謝,這是問題所在! = d – user1165687 2012-07-30 05:36:30

0

我的手我的數據在這樣的接收端

Bundle bundle = this.getIntent().getExtras(); 
    String scan = bundle.getString("scan"); 
    ((TextView)findViewById(R.id.username)).setText(scan); 
7

使用捆綁用於將數據發送到服務。

來自活動。

Bundle bundle = new Bundle(); 
bundle.putCharSequence("extraData", data); 
myIntent.putExtras(bundle); 

接收服務。

Bundle bundle = intent.getExtras(); data = (String) bundle.getCharSequence("extraData");

+0

感謝您的答覆,這看起來像一個良好的向前發展。但是,在我做出更改後,我現在崩潰了: data =(String)bundle.getCharSequence(「extraData」); 它似乎仍然不能傳遞任何東西給我的服務。 – user1165687 2012-07-30 05:33:49

+0

您能否發佈您的錯誤日誌 – Kamalone 2012-07-30 05:35:37

相關問題