我真的尝试了解intent.putExtra()和getIntent().getExtras(),并将它们应用于其中一个SimpleService教程。我知道很多人已经问过“为什么bundle extras总是null?”我保证在发布之前我尝试了在这里找到的答案几个小时,但我认为我不够先进,无法真正理解我可能在做错什么与人们发布的小片段。因此,我放置了我的活动和服务的完整代码。
我认为我的问题是我的起始意图(我在我的活动中创建的那个)不存在于我的服务的上下文中。我想知道也许我完全错误地使用了意图?我确实尝试了在我的服务中使用intent.putExtra,以尝试向另一个方向发送字符串,但那些extras也总是null。因此,冒着重复的风险,为什么bundle extras总是null?如何使单个意图同时存在于我的活动和我的服务的上下文中?
我应该指出,下面显示的代码显然会有一个空的extras,因为我已注释掉了一些失败的尝试来获取.getExtras()。我为了清洁起见删除了其余部分。
编辑:由于回复的答案,以下是代码,供那些已经谷歌了几个小时的人参考。将其放入您的服务中(请注意,返回START_REDELIVER_INTENT可能是错误的):
@Override
public int onStartCommand( Intent intent , int flags , int startId )
{
super.onStartCommand(intent, flags , startId);
extras = intent.getExtras();
//just checking
if( extras != null )
Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
return START_REDELIVER_INTENT;
}
我的活动(基于Sai Geetha的博客):
package com.example.BroadcastIntent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BroadcastIntentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.buttonStart);
start.setOnClickListener(startListener);
Button stop = (Button)findViewById(R.id.buttonStop);
stop.setOnClickListener(stopListener);
//the intent I'm using to start and stop the service -- the extras don't go anywhere....
intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class);
intent.putExtra("extratoservice", "if you can read this, it made it to the service" );
}
Boolean serviceRunning;
Intent intent;
//Clicks from Geetha's Blog
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(intent);
serviceRunning = true;
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
try
{
stopService(intent);
serviceRunning = false;
}
catch( Exception e)
{
Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show();
}
}
};
}
这是我的服务:
package com.example.BroadcastIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class BroadcastService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
//extras = arg0.getExtras(); <-- this has null extras, too...
return null;
}
Bundle extras;
@Override
public void onCreate() {
super.onCreate();
// extras = getIntent().getExtras(); <-- this is undefined?
if( extras == null )
Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show();
}
}
onStartCommand()
吗? - Luis A. Florit