'以下是我的两个课程,我正在尝试将数据从我的活动传递到我的服务。我已经传递给其他活动没有问题,但是当我在我的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();
}
}
这也是我的Manifest文件的所有内容:
<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 :(得分:7)
使用bundle将数据发送到服务。
来自活动。
Bundle bundle = new Bundle();
bundle.putCharSequence("extraData", data);
myIntent.putExtras(bundle);
接受服务。
Bundle bundle = intent.getExtras();
data = (String) bundle.getCharSequence("extraData");
答案 1 :(得分:1)
你的代码看起来很好。
在您从toString()
获取文字时添加EditText
后尝试:
intent.putExtra(MyService.PARAM_IN_NAME,
((EditText)findViewById(R.id.editText1)).getText().toString());
答案 2 :(得分:0)
我把这样的数据交给接收端
Bundle bundle = this.getIntent().getExtras();
String scan = bundle.getString("scan");
((TextView)findViewById(R.id.username)).setText(scan);