2016-05-15 72 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);