Android - 通过 put/getExtra 启动带有额外信息的服务

标签 android service android-intent extra

我确实尝试过 intent.putExtra() 和 getIntent().getExtras() 并将它们应用到其中一个 SimpleService 教程中。我知道很多人已经问过“为什么 bundle extras 总是空的?”我保证在考虑发布之前,我已经尝试破解我在这里找到的答案几个小时,但我认为我还不够先进,无法真正理解人们发布的小 fragment 我一定做错了。因此,我输入了我的 Activity 和服务的完整代码。

我认为我的问题是我的初始 Intent (我在 Activity 中创建的 Intent )在我的服务上下文中不存在。我想知道我是否完全在错误的方向/目的上使用 Intents?我确实在我的服务中尝试了一个 intent.putExtra,试图向另一个方向发送一个字符串,但那些额外的东西也总是空的。所以冒着重复的风险,为什么 bundle extras 总是空的?我如何制定既存在于我的 Activity 上下文中又存在于我的服务中的单一 Intent ?

我应该注意,下面显示的代码显然会有一个 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;

}

我的 Activity (基于 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()功能(我不在桌面上,所以我不能方便地链接到 javadoc)。这将收到您传递的 Intent ,并且您的额外内容应该在那里可用。

关于Android - 通过 put/getExtra 启动带有额外信息的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8112709/

相关文章:

java - 谷歌地图 v2 出错

android - TelephonyManager.getPhoneType 的值是跟phone 和一个固定的常量有关还是跟sim 卡有关?

android - 如何将数据从服务发送到我的 Activity ?

android - 在 ImageView 上用 Canvas 绘图

android - ListView 小部件未在锁定屏幕上更新

objective-c - 无需启动应用程序即可提供 OSX 服务?

android - 前台服务通知始终显示至少 5 秒

Android,使用单个 startActivityForResult 选择和裁剪图像,但检索原始图像 URI?

Android:作为 Intent 启动 Activity 会激活 WakeLock。如何禁用此行为?

android - 短信发送码怎么理解?