代码之家  ›  专栏  ›  技术社区  ›  pgsandstrom

在接到电话时模拟振动

  •  1
  • pgsandstrom  · 技术社区  · 15 年前

    所以我试着模拟手机正在接电话。我已经成功地提取了电话铃声并播放了它。现在我要模拟振动。虽然我可以使手机振动,但我想模拟手机在接到电话时振动的确切模式。是否有一些设置或类可用于提取此模式,并检测是否打开振动?

    3 回复  |  直到 6 年前
        1
  •  4
  •   Donal Rafferty    15 年前

    你必须以一种模式振动它。

    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);  
    
    // 1. Vibrate for 1000 milliseconds  
    long milliseconds = 1000;  
    v.vibrate(milliseconds);  
    
    // 2. Vibrate in a Pattern with 500ms on, 500ms off for 5 times  
    long[] pattern = { 500, 300 };  
    v.vibrate(pattern, 5);
    

    http://www.androidsnippets.org/snippets/22/

    我不确定什么模式被用作标准模式,您可能会在源代码中找到它,或者自己不断尝试不同的模式,直到满意为止。

        2
  •  0
  •   Vadim Kotov First Zero    7 年前

    为什么不使用Android源代码来看看他们是怎么做的?

    手机应用程序源可从
    https://android.googlesource.com/platform/packages/apps/Phone

        3
  •  0
  •   Alexey    6 年前

    似乎没有任何类可以为您的来电提供“Standart”振动设置。 因为大多数手机都有定制的供应商呼叫者应用程序,而且谷歌Play上有很多定制呼叫者应用程序,所以这种“标准”模式可能根本不存在。

    来自AOSP的Standart呼叫者应用程序使用此模式:

    private static final int VIBRATE_LENGTH = 1000; // ms
    private static final int PAUSE_LENGTH = 1000; // ms
    

    检测振动是否开启:

    boolean shouldVibrate() {
        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = audioManager.getRingerMode();
        if (Settings.System.getInt(mContext.getContentResolver(), "vibrate_when_ringing", 0) > 0) {
            return ringerMode != AudioManager.RINGER_MODE_SILENT;
        } else {
            return ringerMode == AudioManager.RINGER_MODE_VIBRATE;
        }
    }
    

    这依赖于“振铃时振动”和“声音模式”设置,您可以在Standart手机设置中找到这些设置。

    推荐文章