2011-09-07 5 views
1

화면이 꺼진 후 내 프로그램이 전화를 진동 시키도록 할 수있는 방법을 찾고 있습니다. 나는 많은 연구를했으며, 효과가있는 것을 찾지 못했습니다. 필자는 PowerManager 클래스와 더 구체적으로 WakeLock 메커니즘을 살펴 보았습니다. 여러 게시물의 소리에서 WakeLock 클래스의 PARTIAL_WAKE_LOCK 변수를 사용해야합니다.화면이 꺼져있을 때 전화 진동 허용

PARTIAL_WAKE_LOCK - CPU가 실행 중인지 확인하는 대기 잠금 장치입니다.

그러나 화면이 꺼지면 휴대 전화를 진동시킬 수 없습니다. SCREEN_DIM_WAKE_LOCK을 사용할 수 있기 때문에 WakeLock을 올바르게 사용하고 있다는 것을 알고 있습니다. PARTIAL_WAKE_LOCK은 (를) 찾고있는 곳입니까?

답변

4
@Override 

    public void onCreate() { 

     super.onCreate(); 

     // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC 

     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 

     filter.addAction(Intent.ACTION_SCREEN_OFF); 

     BroadcastReceiver mReceiver = new ScreenReceiver(); 

     registerReceiver(mReceiver, filter); 

    } 



    @Override 

    public void onStart(Intent intent, int startId) { 

     boolean screenOn = intent.getBooleanExtra("screen_state", false); 

     if (!screenOn) { 

      // Get instance of Vibrator from current Context 
      Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
      // This example will cause the phone to vibrate "SOS" in Morse Code 
      // In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash" 
      // There are pauses to separate dots/dashes, letters, and words 
      // The following numbers represent millisecond lengths 
      int dot = 200;  // Length of a Morse Code "dot" in milliseconds 
      int dash = 500;  // Length of a Morse Code "dash" in milliseconds 
      int short_gap = 200; // Length of Gap Between dots/dashes 
      int medium_gap = 500; // Length of Gap Between Letters 
      int long_gap = 1000; // Length of Gap Between Words 
      long[] pattern = { 
      0, // Start immediately 
      dot, short_gap, dot, short_gap, dot, // s 
      medium_gap, 
      dash, short_gap, dash, short_gap, dash, // o 
      medium_gap, 
      dot, short_gap, dot, short_gap, dot, // s 
      long_gap 
      }; 

      // Only perform this pattern one time (-1 means "do not repeat") 
      v.vibrate(pattern, -1); 


     } else { 

      // YOUR CODE 

     } 

    } 

참고 사항 블록 외부의 Manifest.xml 파일에 사용 권한 행을 추가해야합니다.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="..."> 
<uses-permission android:name="android.permission.VIBRATE"/> 

참고 : 또한이 솔루션은 패턴없이 진동을 직접 사용하는 것이 었습니다, 에뮬레이터가 나를 위해

+0

+1 "에뮬레이터가 진동 할 수 없음":-) – Twinone

+0

충전시 동작이 다르기 때문에 장치에 연결된 USB 케이블없이 테스트하십시오. 너 – userM1433372

1

을 viberate 수없는 실제 전화에이 코드를 테스트해야합니다, 그래서 PowerManager를 사용할 필요가 없습니다 잠에서 깨우기.

+0

맞아. 이것은 잘 돌아갔다. 고마워 – Snicolas

관련 문제