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" />
4

3 回答 3

7

使用 bundle 将数据发送到服务。

从活动。

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

接受服务。

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

于 2012-07-30T05:10:32.807 回答
1

您的代码看起来不错。

toString()当您从以下内容中获取文本时添加后尝试EditText

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

于 2012-07-30T05:25:34.607 回答
0

我像这样把我的数据交给接收端

  Bundle bundle = this.getIntent().getExtras();
    String scan = bundle.getString("scan");
    ((TextView)findViewById(R.id.username)).setText(scan);
于 2012-07-30T05:10:22.840 回答