2013-05-04 34 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

相關問題