【问题标题】:passing intent from Activity to Service fails将意图从 Activity 传递到 Service 失败
【发布时间】:2014-11-08 16:29:22
【问题描述】:

我正在尝试将意图信息从 Activity 传递给 Service,但它不起作用。

在 startService() 之前,我在意图中添加了额外内容。 执行 startService() 时,服务尝试获取 extras 但指针为空

这是我的代码:

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    final ToggleButton btnPlayStop = (ToggleButton) findViewById(R.id.btnPlayStop);
    btnPlayStop.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(getBaseContext(), MyService.class);
            intent.putExtra("song", "umake");
            /*
                Debugger displays:
                    intent.mExtras.mMap.table[1]
                        key = "song"
                        value = "umake"
             */

            startService(intent);
        }
    }
}

public class MyService extends Service
{
public int onStartCommand(Intent intent, int flags, int startId)
{
    try
    {
        Toast.makeText(this, "MyService Started", Toast.LENGTH_LONG).show();

       // intent = getIntent();                 
       // --> compiler error: The method getIntent() is undefined for the type MyService

        Bundle extras = intent.getExtras();
        String song = extras.getString("song");  // nope

        song = intent.getStringExtra("song");  // nope
        Bundle bundle = intent.getBundleExtra("song");  // nope
        song = bundle.getString("song");  // nope

        /*
            Neither of the above works since the map in extras is null
            Debugger displays:
                intent.mExtras.mMap == null

            whereas before startService is was filled all right
         */
    }...

就好像数据在启动服务期间“丢失”了一样。 如何解决?

谢谢

克里斯

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    在服务中试试这些代码

     String song=intent.getStringExtra("song");
    

    【讨论】:

      【解决方案2】:

      这是因为你没有得到捆绑包中的字符串值。 尝试添加 extras.getString("")

      【讨论】:

        【解决方案3】:

        如何使用

        String song = intent.getBundleExtra("song");
        

        【讨论】:

        • intent.getBundleExtra("song") 被定义为返回 Bundle 类型的东西而不是 String 我是否使用了错误/已弃用的 API?您使用的是什么 API?
        【解决方案4】:

        在您的 MainActivity.java 中,您声明您的意图,尝试拥有

         Intent intent = new Intent(v.getContext(), MyService.class);
        

        然后在 MyService.java 中接收您的意图,尝试拥有

        public int onStartCommand(Intent intent, int flags, int startId)
        {
          try
          {
              Toast.makeText(this, "MyService Started", Toast.LENGTH_LONG).show();
              intent = getIntent(); //Here You Want to make sure you are actually getting values from the previous activity
              Bundle extras = intent.getExtras();
            /*
                Debugger displays:
                    intent.mExtras.mMap == null
             */
        }
        

        我认为你没有正确初始化你的意图,这就是为什么它一直给你那个错误

        【讨论】:

        • @ChrisPeeters 接受答案,如果此代码对您有用,如果没有,请告诉我这里有什么错误
        • intent = getIntent() 对我不起作用 --> "方法 getIntent() 对于 MyService 类型未定义"
        猜你喜欢
        • 1970-01-01
        • 2011-03-18
        • 2012-04-14
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多