Ada*_*dam 10 service android android-intent extra
我真的试图通过intent.putExtra()和getIntent().getExtras()并将它们应用到SimpleService教程之一.我知道很多人已经问过"为什么捆绑附加内容总是为空?" 我保证我试图通过我发现这里几个小时,我考虑在发布前的答案破解,但我不认为我不够先进,以真正了解它是什么,我必须做错误的未成年人片段张贴.因此,我输入了我的活动和服务的完整代码.
我认为我的问题是我的开始意图(我在我的活动中创建的那个)在我的服务环境中不存在.我想知道我是否完全在错误的方向/目的上使用Intents?我在我的服务中尝试了一个intent.putExtra,尝试向另一个方向发送一个字符串,但这些额外的东西也总是为空.所以冒着重复的风险,为什么捆绑附加内容总是为空?如何在我的活动和服务的上下文中创建一个单一的意图?
我应该注意下面显示的代码显然会有一个空的额外内容,因为我已经注释了一些失败的.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;
}
Run Code Online (Sandbox Code Playgroud)
我的活动(基于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();
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务:
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();
}
}
Run Code Online (Sandbox Code Playgroud)