2016-05-15 61 views
0

Android的震動我想建立一個簡單的應用程序,用於振動上點擊複選框後再次點擊停止:上覆選框

目前它看起來像這樣:

import android.content.Context; 
import android.os.Vibrator; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Vibrator vibrator = (Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE); 
     final CheckBox vibrateCheckBox = (CheckBox) findViewById(R.id.checkPowerStrong); 

     if(vibrateCheckBox.isChecked()) { 
      while(vibrateCheckBox.isChecked()) { 
       vibrator.vibrate(1000); 
      } 
     } else { 
      vibrator.cancel(); 
     } 

    } 
} 

但是我得到一個錯誤:

Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

我給清單允許振動:

如何解決這個

+0

也許添加一個更改偵聽器到複選框?因爲這告訴它在應用程序啓動時立即振動,而不是在檢查框時 –

+1

您不應該使用該代碼獲取該Exception。 –

+0

你能告訴我一個關於你的確切含義的片段嗎 –

回答

1

它的工作原理正是你想要的。做這樣的事情

final Vibrator vibrator = (Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE); 
final CheckBox vibrateCheckBox = (CheckBox) findViewById(R.id.checkPowerStrong); 

final Handler handler = new Handler(); 

final Runnable r = new Runnable() { 
    public void run() { 
     vibrator.vibrate(1000); 
     handler.postDelayed(this, 1000); 
    } 
}; 

vibrateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
      if(vibrateCheckBox.isChecked()) { 
       handler.postDelayed(r, 100); 
      } else { 
       handler.removeCallbacks(r); 
       vibrator.cancel(); 
      } 

     } 
    } 
); 
+0

這個工作很漂亮 –

+0

你能解釋一下我的錯誤是什麼 –

+0

Upvote是appriciated。 – Masum

0
public class MainActivity extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

vibrateCheckBox = (CheckBox)findViewById(R.id.checkPowerStrong); 
vibrateCheckBox.setChecked(false); 
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE); 



vibrateCheckBox .setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) 
     { 
      v.vibrate(500);  // Vibrate for 500 milliseconds 
     }else 
     { 
      v.cancel(); 
     } 
    } 
}); 

} 

添加權限的Manifest.xml

<uses-permission android:name="android.permission.VIBRATE"/> 

如何震動無限期

// Get instance of Vibrator from current Context 
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

// Start without a delay 
// Vibrate for 100 milliseconds 
// Sleep for 1000 milliseconds 
long[] pattern = {0, 100, 1000}; 

// The '0' here means to repeat indefinitely 
// '0' is actually the index at which the pattern keeps repeating from (the start) 
// To repeat the pattern from any other point, you could increase the index, e.g. '1' 
v.vibrate(pattern, 0); 

當你準備停止振動,只需調用cancel()方法:

v.cancel(); 

如何使用振動模式

If you want a more bespoke vibration, you can attempt to create your own vibration patterns: 

// Get instance of Vibrator from current Context 
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

// Start without a delay 
// Each element then alternates between vibrate, sleep, vibrate, sleep... 
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100}; 

// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array 
v.vibrate(pattern, -1);