'下面是我试图将数据从我的活动传递到我的服务的两个类。我已经传递给其他活动没问题,但是当我在 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" />