2014-10-14 1 views
0

내 응용 프로그램을 3 초 동안 진동 시키지만 작동하지 않아 그 이유를 알 수 없습니다. 버튼을 클릭해도 아무런 변화가 없습니다. 처음에는 주 활동에서 시도했지만 동일한 결과가있었습니다.3 초 동안 장치를 진동시킬 수 없음

주요 활동

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_my); 
    final Button vibrateButton = new Button(this); 
    final Intent vibration = new Intent(this, MyService.class); 

    vibrateButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startService(vibration); 
     } 
    }); 
} 

서비스

public class MyService extends IntentService { 
    public MyService() 
    { 
     super("MyService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent vibration) 
    { 
     Vibrator vibe = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); 
     vibe.vibrate(3000); 
    } 
} 
+1

당신이 Manifest.xml의 권한을 가지고 있습니까? notthetup

답변

2

시도 :

import android.os.Vibrator; 
... 
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE); 
// Vibrate for 3000 milliseconds 
v.vibrate(3000); 

참고 :

의 AndroidManifest.xml 파일에 권한을 포함하는 것을 잊지 마십시오

<uses-permission android:name="android.permission.VIBRATE"/> 
관련 문제