2013-05-04 40 views
-1

我创造的意图,并添加一个额外的参数吧:的Android getExtra总是返回null

public void stop(View v) { 
    AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); 
    int currentVolume = am.getStreamVolume(AudioManager.STREAM_RING); 
    Intent intent = new Intent(); 
    intent.setAction(CANCEL_ACTION); 
    intent.putExtra("volume", currentVolume); 
    sendBroadcast(intent); 
} 

我得到了我的广播接收器的意图,但“量”总是空:

public void onReceive(Context context, Intent intent) { 
    if (intent != null) { 
     if (intent.getAction().equals("com.hamm.ringeroff.cancel")) { 
      String _volume = intent.getStringExtra("volume"); 
     } 
    } 
} 

我错过了什么?它必须是简单的。谢谢。

+0

你肯定'currentVolume'变量是从来没有空? – 2013-05-04 03:07:15

+0

只给你在这里发布的代码'currentVolume'没有实例化,因此是'null',所以这将是预期的行为......实际上'currentVolume'甚至没有在这个代码中声明,所以这不应该甚至编译。请在声明和实例化'currentVolume'的地方张贴代码部分。您可以采取的调试步骤是将其打印到日志中,然后将其添加到意图中,这会明确告诉您添加它时的价值。 – FoamyGuy 2013-05-04 03:08:47

+0

什么是currentVolume,int的数据类型? – Sudhee 2013-05-04 05:38:03

回答

1

要么currentVolume是字符串和NULL,或者为int在这种情况下,使用getIntExtra

相关问题