2010-08-16 154 views
0

我試圖通過使用getRingerMode()方法來確定當前的鈴聲狀態。getRingerMode()總是返回值0

我創建了一個變量: private int currentRingerMode;

將我的變量設置爲: currentRingerMode = getRingerMode();

和使用方法如下:
私人INT getRingerMode(){// TODO自動生成方法存根 返回currentRingerMode; }

但是,當我記錄變量的值: Log.v(TAG,「value =」+ currentRingerMode);

無論鈴聲狀態如何,總是返回0的值。我究竟做錯了什麼?

謝謝。

-Brian


-Update 這是我目前的全部代碼。

package com.DoNotDisturb.widget; 

import com.DoNotDisturb.widget.R;

import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.media.AudioManager; import android.util.Log; import android.widget.RemoteViews; import android.widget.Toast;

公共類DoNotDisturbWidget當你聲明private int currentRingerMode;延伸的AppWidgetProvider {

private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget"; 
private static final String TAG = null; 
private int currentRingerMode; 
private int ringerMode; 
private RemoteViews views = new RemoteViews("com.DoNotDisturb.widget", R.layout.main); 

private AudioManager getSystemService(String audioService) { 
    // TODO Auto-generated method stub 

    return null; 
    } 


@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    Intent intent = new Intent(context, DoNotDisturbWidget.class); 
    intent.setAction(ACTION_WIDGET_RECEIVER); 


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
    views.setOnClickPendingIntent(R.id.button_one, pendingIntent); 

    appWidgetManager.updateAppWidget(appWidgetIds, views); 

    AudioManager mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE); 
    int ringerMode = mAudioManager.getRingerMode(); 
    Log.d(TAG, "ringerMode value=" + ringerMode); 

    ContextWrapper mContext = null; 
    AudioManager manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 
    currentRingerMode = manager.getRingerMode(); 
    Log.d(TAG, "ringerMode value=" + currentRingerMode); 

} 



@Override 
public void onReceive(Context context, Intent intent) { 

    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)){ 

     Log.d(TAG, "ringerMode value=" + ringerMode); 

     try { 

      //Silent 
      if(currentRingerMode == 2){ 
      Toast.makeText(context, "Silent", Toast.LENGTH_SHORT).show(); 
      ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_SILENT); 
      Log.v(TAG, "After ButtonPress value=" + currentRingerMode); 


      } 

      //Vibrate 
       if(currentRingerMode == 1){ 
       Toast.makeText(context, "Normal", Toast.LENGTH_SHORT).show(); 
       ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
       Log.v(TAG, "After ButtonPress value=" + currentRingerMode); 


       } 

      //Normal Mode 
      if(currentRingerMode == 0){ 
       Toast.makeText(context, "Silent", Toast.LENGTH_SHORT).show(); 
       ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_SILENT); 
       Log.v(TAG, "After ButtonPress value=" + currentRingerMode); 

      } 
     } 

     finally{} 

    } 

    super.onReceive(context, intent); 
} 

}

回答

0

currentRingerMode默認值是0

而且因爲你在呼喚getRingerMode(),顯然不是從一個AudioManager對象,你的方法

private int getRingerMode() { 
    // TODO Auto-generated method stub return currentRingerMode; 
    return currentRingerMode; 
} 

被調用,因此0所有的時間

UNTESTED

因此,首先,您必須檢索音頻服務

AudioManager manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 

,並從其中調用getRingerMode()

currentRingerMode = manager.getRingerMode(); 

編輯 如果你是一個活動裏面,下面應該工作

private int currentRingerMode; 
AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
currentRingerMode = manager.getRingerMode(); 

EDIT 2

在你的onUpdate,試試這個

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    Intent intent = new Intent(context, DoNotDisturbWidget.class); 
    intent.setAction(ACTION_WIDGET_RECEIVER); 


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
    views.setOnClickPendingIntent(R.id.button_one, pendingIntent); 

    appWidgetManager.updateAppWidget(appWidgetIds, views); 

    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
    currentRingerMode = manager.getRingerMode(); 
    Log.d(TAG, "ringerMode value=" + currentRingerMode); 

} 

此外,看看LogCat並確保您有AUDIO權限。

+0

另外不要忘記設置訪問音頻服務所需的權限 – ccheneson 2010-08-18 09:05:51

+0

感謝您的答覆。所以如果我正確地理解它,我仍然像我以前那樣用相同的方式聲明我的變量,但是我的代碼應該看起來像這樣: ContextWrapper mContext = null; \t \t AudioManager manager =(AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); \t \t currentRingerMode = manager.getRingerMode(); \t \t Log.d(TAG,「ringerMode value =」+ currentRingerMode); 謝謝。 – 2010-08-18 20:17:55

+0

你從mContext調用getSystemService是null(我不知道ContextWrapper和Context的區別)。你必須從有效的上下文(例如一個活動)中調用它。除此之外,其餘部分都是正確的。 – ccheneson 2010-08-18 20:37:09