我想我的应用程序振动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);
}
}发布于 2014-10-14 07:52:21
尝试:
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"/>https://stackoverflow.com/questions/26355396
复制相似问题