如何使用C#访问Windows Phone 7的静音/振动模式?

时间:2012-04-14 04:34:13

标签: windows-phone-7 silent vibrate

我能够在“aygshell.dll”上使用p / invoke来访问Windows Mobile 6中的手机声音配置文件,但Windows Phone 7不支持以下代码。有没有解决的办法?我希望我的应用程序能够以静音或振动模式设置手机。

/*The following code works perfectly well with windows moblile 6.0 but fails for 
  windows phone 7 at runtime. */



  public enum SND_SOUNDTYPE
   {
       On,
       File,
       Vibrate,
       None
   }

   private enum SND_EVENT
   {
       All,
       RingLine1,
       RingLine2,
       KnownCallerLine1,
       RoamingLine1,
       RingVoip
   }
 //Marshals
[StructLayout(LayoutKind.Sequential)]
   private struct SNDFILEINFO
   {
       [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
       public string szPathName;
       [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
       public string szDisplayName;
       public SND_SOUNDTYPE sstType;
   }

//p/invoke
   [DllImport("aygshell.dll", SetLastError = true)]
    private static extern uint SndSetSound(SND_EVENT seSoundEvent, ref SNDFILEINFO pSoundFileInfo, bool fSuppressUI);

   [DllImport("aygshell.dll", SetLastError = true)]
   private static extern uint SndGetSound(SND_EVENT seSoundEvent, ref SNDFILEINFO pSoundFileInfo);


  //method to set ringer on 
   private static void SetProfileNormal()
   {
       SNDFILEINFO soundFileInfo = new SNDFILEINFO();
       soundFileInfo.sstType = SND_SOUNDTYPE.On;
       SndSetSound(SND_EVENT.All, ref soundFileInfo, true);

   }
 //method to set ringer to vibrate
   private static void SetProfileVibrate()
   {
       SNDFILEINFO soundFileInfo = new SNDFILEINFO();
       soundFileInfo.sstType = SND_SOUNDTYPE.Vibrate;
       SndSetSound(SND_EVENT.All, ref soundFileInfo, true);

   }

  //method to set ringer off - silent mode
  private static void SetProfileMuted()
   {
       SNDFILEINFO soundFileInfo = new SNDFILEINFO();
       soundFileInfo.sstType = SND_SOUNDTYPE.None;
       SndSetSound(SND_EVENT.All, ref soundFileInfo, true);

   }
 //method to check if phone is in vibrate mode
   private bool IsInVibrateMode()
   {
       SNDFILEINFO info = new SNDFILEINFO();
       SndGetSound(SND_EVENT.All, ref info);
       return (info.sstType == SND_SOUNDTYPE.Vibrate);
   }
 //method to check if phone is in silent mode
   private bool IsMuted()
   {
       SNDFILEINFO info = new SNDFILEINFO();
       SndGetSound(SND_EVENT.All, ref info);
       return (info.sstType == SND_SOUNDTYPE.None);
   }

1 个答案:

答案 0 :(得分:1)

Windows Phone 7的安全沙箱不允许开发者访问将手机模式设置为静音或振动。最好的情况是,该应用程序可以调出设置菜单,以允许用户亲自将手机设置为静音或振动模式。